<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/blog/","images":"/blog/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="一年春又来">
<meta property="og:url" content="http://littlefxc.github.io/page/2/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="一年春又来">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/page/2/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>
<title>一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">184</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/02/SpringBoot%E9%9B%86%E6%88%90jasypt%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E5%8A%A0%E5%AF%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/06/02/SpringBoot%E9%9B%86%E6%88%90jasypt%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E5%8A%A0%E5%AF%86/" class="post-title-link" itemprop="url">SpringBoot集成jasypt实现配置加密</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-06-02 19:32:08 / 修改时间：19:46:56" itemprop="dateCreated datePublished" datetime="2021-06-02T19:32:08+08:00">2021-06-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/java-spring/" itemprop="url" rel="index"><span itemprop="name">java/spring</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <blockquote>
<p>转载自 <a target="_blank" rel="noopener" href="https://blog.lqdev.cn/2019/05/08/springboot/chapter-thirty-seven/">https://blog.lqdev.cn/2019/05/08/springboot/chapter-thirty-seven/</a></p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><blockquote>
<p>近期在进行项目安全方面评审时，质量管理部门有提出需要对配置文件中的敏高文件进行加密处理，避免了信息泄露问题。想想前段时间某公司上传github时，把相应的生产数据库明文密码也一并上传了，导致了相应的数据泄露问题。也确实，大部分项目无论开发、测试还是生产环境，相关的敏高信息都是明文存储的，也是一大安全隐患呀。所以今天来说说，如何对配置文件进行加密操作。</p>
</blockquote>
<h2 id="一点知识"><a href="#一点知识" class="headerlink" title="一点知识"></a>一点知识</h2><h3 id="何为Jasypt"><a href="#何为Jasypt" class="headerlink" title="何为Jasypt"></a>何为Jasypt</h3><blockquote>
<p><a target="_blank" rel="noopener" href="http://jasypt.org/">Jasypt</a>是一个Java库，允许开发人员以很简单的方式添加基本加密功能，而无需深入研究加密原理。利用它可以实现高安全性的，基于标准的加密技术，无论是单向和双向加密。加密密码，文本，数字，二进制文件。</p>
</blockquote>
<ol>
<li>高安全性的，基于标准的加密技术，无论是单向和双向加密。加密密码，文本，数字，二进制文件…</li>
<li>集成Hibernate的。</li>
<li>可集成到Spring应用程序中，与Spring Security集成。</li>
<li>集成的能力，用于加密的应用程序（即数据源）的配置。</li>
<li>特定功能的高性能加密的multi-processor/multi-core系统。</li>
<li>与任何JCE提供者使用开放的API</li>
</ol>
<p>官网：<a target="_blank" rel="noopener" href="http://www.jasypt.org/">http://www.jasypt.org/</a></p>
<h2 id="SpringBoot集成Jasypt"><a href="#SpringBoot集成Jasypt" class="headerlink" title="SpringBoot集成Jasypt"></a>SpringBoot集成Jasypt</h2><blockquote>
<p><code>SpringBoot</code>中集成<code>Jasypt</code>，可直接使用开源的<code>jasypt-spring-boot</code>直接集成，使用简单方便。</p>
</blockquote>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/WTvOjzNR7Xt5.png" alt="mark"></p>
<h3 id="常规集成示例"><a href="#常规集成示例" class="headerlink" title="常规集成示例"></a>常规集成示例</h3><ol>
<li>引入pom依赖</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.ulisesbocchio<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jasypt-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>设置盐值和修改相应需要加密的配置参数</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 需要解密的地方，使用ENC()进行包裹处理</span><br><span class="line">okong.name&#x3D;ENC(Xj7Ykn2O0Hni&#x2F;tN4oojPfw&#x3D;&#x3D;)</span><br><span class="line"></span><br><span class="line"># 设置盐值，生产环境中，切记不要直接进行设置，可通过环境变量、命令行等形式进行设置。</span><br><span class="line">jasypt.encryptor.password&#x3D;lqdev</span><br></pre></td></tr></table></figure>

<p>简单来说，就是在需要加密的值使用<code>ENC(</code>和<code>)</code>进行包裹，即：<code>ENC(密文)</code>。若想避免参数冲突，可修改前缀和后缀，可以直接使用<code>jasypt.encryptor.property.prefix</code>和<code>jasypt.encryptor.property.suffix</code>进行修改即可。</p>
<p><strong>之后想往常一样使用<code>@Value(&quot;$&#123;&#125;&quot;)</code>即可。</strong></p>
<h3 id="包含xml引入时"><a href="#包含xml引入时" class="headerlink" title="包含xml引入时"></a>包含xml引入时</h3><blockquote>
<p>在一些使用<code>javaBean</code>配置和<code>xml</code>两种混合模式时，使用第一种配置时，<code>xml</code>参数并未替换。此时看了官方文档，可以使用另一方式进行配置即可。</p>
</blockquote>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/bPoPUpE8uk4A.png" alt="官方说明"></p>
<ol>
<li><p>引入pom依赖</p>
 <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.ulisesbocchio<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jasypt-spring-boot<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p> 其实就是不进行自动配置而已。</p>
</li>
<li><p>启动类启动方式修改。</p>
 <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JasyptApplication</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// SpringApplication.run(JasyptApplication.class, args);</span></span><br><span class="line">    <span class="comment">// 使用自定义环境变量 实现一些特殊场景下的加密字符解密操作</span></span><br><span class="line">    <span class="comment">// 若无额外的xml引入文件需要解密时，可直接使用SpringApplication.run(JasyptApplication.class, args);即可</span></span><br><span class="line">    <span class="comment">// 若想在引入的xml中使用，需要加入环境变量，如以下模式</span></span><br><span class="line">    <span class="keyword">new</span> SpringApplicationBuilder().environment(<span class="keyword">new</span> StandardEncryptableEnvironment())</span><br><span class="line">    .sources(JasyptApplication.class).run(args);</span><br><span class="line">    log.info(<span class="string">&quot;spring-boot-jasypt-chapter37服务启动!&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="其他配置项"><a href="#其他配置项" class="headerlink" title="其他配置项"></a>其他配置项</h3><table>
<thead>
<tr>
<th>Key</th>
<th>Required</th>
<th>Default Value</th>
</tr>
</thead>
<tbody><tr>
<td>jasypt.encryptor.password</td>
<td><strong>True</strong></td>
<td>盐值，根密码</td>
</tr>
<tr>
<td>jasypt.encryptor.algorithm</td>
<td>False</td>
<td>PBEWithMD5AndDES</td>
</tr>
<tr>
<td>jasypt.encryptor.keyObtentionIterations</td>
<td>False</td>
<td>1000</td>
</tr>
<tr>
<td>jasypt.encryptor.poolSize</td>
<td>False</td>
<td>1</td>
</tr>
<tr>
<td>jasypt.encryptor.providerName</td>
<td>False</td>
<td>SunJCE</td>
</tr>
<tr>
<td>jasypt.encryptor.providerClassName</td>
<td>False</td>
<td>null</td>
</tr>
<tr>
<td>jasypt.encryptor.saltGeneratorClassname</td>
<td>False</td>
<td>org.jasypt.salt.RandomSaltGenerator</td>
</tr>
<tr>
<td>jasypt.encryptor.ivGeneratorClassname</td>
<td>False</td>
<td>org.jasypt.salt.NoOpIVGenerator</td>
</tr>
<tr>
<td>jasypt.encryptor.stringOutputType</td>
<td>False</td>
<td>base64</td>
</tr>
<tr>
<td>jasypt.encryptor.proxyPropertySources</td>
<td>False</td>
<td>false</td>
</tr>
</tbody></table>
<h2 id="运维说明"><a href="#运维说明" class="headerlink" title="运维说明"></a>运维说明</h2><p>为了方便运维人员对各类敏感密钥进行加密操作，提供了自动化脚本，方便生成相应的加密串。</p>
<h3 id="密钥（盐值）存储说明"><a href="#密钥（盐值）存储说明" class="headerlink" title="密钥（盐值）存储说明"></a>密钥（盐值）存储说明</h3><p>本身加解密过程都是通过<code>盐值</code>进行处理的，所以正常情况下<code>盐值</code>和<code>加密串</code>是分开存储的。**<code>盐值</code>应该放在<code>系统属性</code>、<code>命令行</code>或是<code>环境变量</code>来使用，而不是放在配置文件。**</p>
<h4 id="命令行示例"><a href="#命令行示例" class="headerlink" title="命令行示例"></a>命令行示例</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar xxx.jar --jasypt.encryptor.password=xxx &amp;</span><br></pre></td></tr></table></figure>

<h4 id="环境变量示例"><a href="#环境变量示例" class="headerlink" title="环境变量示例"></a>环境变量示例</h4><p>设置环境变量：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/profileexport JASYPT_PASSWORD = xxxx</span><br></pre></td></tr></table></figure>

<p>启动命令：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar xxx.jar --jasypt.encryptor.password=<span class="variable">$&#123;JASYPT_PASSWORD&#125;</span> &amp;</span><br></pre></td></tr></table></figure>

<h3 id="bat脚本"><a href="#bat脚本" class="headerlink" title="bat脚本"></a>bat脚本</h3><p>为了方便，简单编写了一个bat脚本方便使用。</p>
<figure class="highlight bat"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">set</span>/p input=待加密的明文字符串：</span><br><span class="line"><span class="built_in">set</span>/p password=加密密钥(盐值)：</span><br><span class="line"><span class="built_in">echo</span> 加密中......</span><br><span class="line">java -cp jasypt-<span class="number">1</span>.<span class="number">9</span>.<span class="number">2</span>.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI  input=<span class="variable">%input%</span> password=<span class="variable">%password%</span> algorithm=PBEWithMD5AndDES</span><br><span class="line"><span class="built_in">pause</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：<code>jasypt-1.9.2.jar</code> 文件需要和bat脚本放在相同目录下。此包可直接在示例项目中直接下载。</strong></p>
<p>使用示例：</p>
<p><strong>注意：相应加密串，每次加密的结果是不同的。</strong></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/568wfkAWA5Tl.png" alt="使用示例"></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本章节主要简单介绍了如何使用<code>jasypt</code>对配置文件进行加密操作。一些其他高级应用，可以查看官方文档进行相关集成即可。集成起来相对来说比较简单，注意是要对<code>密码(盐值)</code>的管理，需要进行安全把控下，建议运维人员针对每个项目进行不一样的盐值操作，避免一个项目泄露了，造成其他关联项的信息泄露。安全无大小呀，还是谨慎为妙！</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><ol>
<li><a target="_blank" rel="noopener" href="https://github.com/ulisesbocchio/jasypt-spring-boot">https://github.com/ulisesbocchio/jasypt-spring-boot</a></li>
<li><a target="_blank" rel="noopener" href="http://www.jasypt.org/">http://www.jasypt.org/</a></li>
</ol>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/01/MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/06/01/MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/" class="post-title-link" itemprop="url">MyCat概述与基本概念</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-06-01 13:00:02" itemprop="dateCreated datePublished" datetime="2021-06-01T13:00:02+08:00">2021-06-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-06-05 14:39:55" itemprop="dateModified" datetime="2021-06-05T14:39:55+08:00">2021-06-05</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/" itemprop="url" rel="index"><span itemprop="name">分库分表</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="什么是MyCat"><a href="#什么是MyCat" class="headerlink" title="什么是MyCat"></a>什么是MyCat</h2><p>MyCat 是什么？从定义和分类来看，它是一个开源的分布式数据库系统，前端的用户可以把它看成一个数据库代理，用MySql客户端和命令行工具都可以访问，而其后端则是用MySql原生的协议与多个MySql服务之间进行通信。MyCat的核心功能是分库分表，即将一个大表水平切分成N个小表，然后存放在后端的MySql数据当中。</p>
<p>MyCat发展到目前的版本，已经不是一个单纯的MySql代理了，它的后端支持MySql，Oracle，SqlServer，DB2等主流的数据库，也支持MongoDB这种NoSql数据库。而对于前端的用户来说，无论后端采用哪一种数据库，在MyCat里都是一个传统的数据库，支持标准的SQL语句，对于前端的开发人员来说，可以大大地降低开发难度，提升开发速度。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/blog/2021/06/01/MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/01/%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A1%AE%E7%9A%84%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/06/01/%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A1%AE%E7%9A%84%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB/" class="post-title-link" itemprop="url">如何正确的使用数据库读写分离</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-06-01 09:03:00 / 修改时间：09:13:41" itemprop="dateCreated datePublished" datetime="2021-06-01T09:03:00+08:00">2021-06-01</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><p>在应用系统发展的初期，我们并不知道以后会发展成什么样的规模，所以一开始不会考虑复杂的系统架构，复杂的系统架构费时费力，开发周期长，与系统发展初期这样的一个定位是不吻合的。所以，我们都会采用简单的架构，随着业务不断的发展，访问量不断升高，我们再对系统进行架构方面的优化。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/blog/2021/06/01/%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A1%AE%E7%9A%84%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/05/28/Java%20NIO%E4%B9%8BSelector%EF%BC%88%E9%80%89%E6%8B%A9%E5%99%A8)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/05/28/Java%20NIO%E4%B9%8BSelector%EF%BC%88%E9%80%89%E6%8B%A9%E5%99%A8)/" class="post-title-link" itemprop="url">Java NIO之Selector（选择器）</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-05-28 19:39:32 / 修改时间：09:08:06" itemprop="dateCreated datePublished" datetime="2021-05-28T19:39:32+08:00">2021-05-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/Netty/" itemprop="url" rel="index"><span itemprop="name">Netty</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Java-NIO之Selector（选择器）"><a href="#Java-NIO之Selector（选择器）" class="headerlink" title="Java NIO之Selector（选择器）"></a>Java NIO之Selector（选择器）</h1><blockquote>
<p>转载自<a target="_blank" rel="noopener" href="https://www.cnblogs.com/snailclimb/p/9086334.html">https://www.cnblogs.com/snailclimb/p/9086334.html</a></p>
</blockquote>
<h2 id="一-Selector（选择器）介绍"><a href="#一-Selector（选择器）介绍" class="headerlink" title="一 Selector（选择器）介绍"></a><strong>一 Selector（选择器）介绍</strong></h2><p><strong>Selector</strong> 一般称 为<strong>选择器</strong> ，当然你也可以翻译为 <strong>多路复用器</strong> 。它是Java NIO核心组件中的一个，用于检查一个或多个NIO Channel（通道）的状态是否处于可读、可写。如此可以实现单线程管理多个channels,也就是可以管理多个网络链接。</p>
<p><a target="_blank" rel="noopener" href="https://user-gold-cdn.xitu.io/2018/5/15/16363f5338f36c54?w=636&h=260&f=png&s=23373">https://user-gold-cdn.xitu.io/2018/5/15/16363f5338f36c54?w=636&amp;h=260&amp;f=png&amp;s=23373</a></p>
<p><strong>使用Selector的好处在于：</strong> 使用更少的线程来就可以来处理通道了， 相比使用多个线程，避免了线程上下文切换带来的开销。</p>
<h2 id="二-Selector（选择器）的使用方法介绍"><a href="#二-Selector（选择器）的使用方法介绍" class="headerlink" title="二 Selector（选择器）的使用方法介绍"></a><strong>二 Selector（选择器）的使用方法介绍</strong></h2><h3 id="1-Selector的创建"><a href="#1-Selector的创建" class="headerlink" title="1. Selector的创建"></a><strong>1. Selector的创建</strong></h3><p>通过调用Selector.open()方法创建一个Selector对象，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Selector selector &#x3D; Selector.open();</span><br></pre></td></tr></table></figure>

<p>这里需要说明一下</p>
<h3 id="2-注册Channel到Selector"><a href="#2-注册Channel到Selector" class="headerlink" title="2. 注册Channel到Selector"></a><strong>2. 注册Channel到Selector</strong></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">channel.configureBlocking(false);</span><br><span class="line">SelectionKey key &#x3D; channel.register(selector, Selectionkey.OP_READ);</span><br></pre></td></tr></table></figure>

<p><strong>Channel必须是非阻塞的</strong>。 所以FileChannel不适用Selector，因为FileChannel不能切换为非阻塞模式，更准确的来说是因为FileChannel没有继承SelectableChannel。Socket channel可以正常使用。</p>
<p><strong>SelectableChannel抽象类</strong> 有一个 <strong>configureBlocking（）</strong> 方法用于使通道处于阻塞模式或非阻塞模式。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">abstract SelectableChannel configureBlocking(boolean block)</span><br></pre></td></tr></table></figure>

<h3 id="注意："><a href="#注意：" class="headerlink" title="注意："></a><strong>注意：</strong></h3><p><strong>SelectableChannel抽象类</strong>的<strong>configureBlocking（）</strong> 方法是由 <strong>AbstractSelectableChannel抽象类</strong>实现的，<strong>SocketChannel、ServerSocketChannel、DatagramChannel</strong>都是直接继承了 <strong>AbstractSelectableChannel抽象类</strong> 。 </p>
<p><strong>register()</strong> 方法的第二个参数。这是一个“ <strong>interest集合</strong> ”，意思是在<strong>通过Selector监听Channel时对什么事件感兴趣</strong>。可以监听四种不同类型的事件：</p>
<ul>
<li><strong>Connect</strong></li>
<li><strong>Accept</strong></li>
<li><strong>Read</strong></li>
<li><strong>Write</strong></li>
</ul>
<p>通道触发了一个事件意思是该事件已经就绪。比如某个Channel成功连接到另一个服务器称为“ <strong>连接就绪</strong> ”。一个Server Socket Channel准备好接收新进入的连接称为“ <strong>接收就绪</strong> ”。一个有数据可读的通道可以说是“ <strong>读就绪</strong> ”。等待写数据的通道可以说是“ <strong>写就绪</strong> ”。</p>
<p>这四种事件用SelectionKey的四个常量来表示：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">SelectionKey.OP_CONNECT</span><br><span class="line">SelectionKey.OP_ACCEPT</span><br><span class="line">SelectionKey.OP_READ</span><br><span class="line">SelectionKey.OP_WRITE</span><br></pre></td></tr></table></figure>

<p>如果你对不止一种事件感兴趣，使用或运算符即可，如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;</span><br></pre></td></tr></table></figure>

<h3 id="3-SelectionKey介绍"><a href="#3-SelectionKey介绍" class="headerlink" title="3. SelectionKey介绍"></a><strong>3. SelectionKey介绍</strong></h3><p>一个SelectionKey键表示了一个特定的通道对象和一个特定的选择器对象之间的注册关系。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">key.attachment(); <span class="comment">//返回SelectionKey的attachment，attachment可以在注册channel的时候指定。</span></span><br><span class="line">key.channel(); <span class="comment">// 返回该SelectionKey对应的channel。</span></span><br><span class="line">key.selector(); <span class="comment">// 返回该SelectionKey对应的Selector。</span></span><br><span class="line">key.interestOps(); <span class="comment">//返回代表需要Selector监控的IO操作的bit mask</span></span><br><span class="line">key.readyOps(); <span class="comment">// 返回一个bit mask，代表在相应channel上可以进行的IO操作。</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>key.interestOps():</p>
</blockquote>
<p>我们可以通过以下方法来判断Selector是否对Channel的某种事件感兴趣</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int interestSet &#x3D; selectionKey.interestOps();</span><br><span class="line">boolean isInterestedInAccept &#x3D; (interestSet &amp; SelectionKey.OP_ACCEPT) &#x3D;&#x3D; SelectionKey.OP_ACCEPT；</span><br><span class="line">boolean isInterestedInConnect &#x3D; interestSet &amp; SelectionKey.OP_CONNECT;</span><br><span class="line">boolean isInterestedInRead &#x3D; interestSet &amp; SelectionKey.OP_READ;</span><br><span class="line">boolean isInterestedInWrite &#x3D; interestSet &amp; SelectionKey.OP_WRITE;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>key.readyOps()</p>
</blockquote>
<p>ready 集合是通道已经准备就绪的操作的集合。JAVA中定义以下几个方法用来检查这些操作是否就绪.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;创建ready集合的方法int readySet &#x3D; selectionKey.readyOps();</span><br><span class="line">&#x2F;&#x2F;检查这些操作是否就绪的方法</span><br><span class="line">key.isAcceptable();&#x2F;&#x2F;是否可读，是返回 trueboolean isWritable()：&#x2F;&#x2F;是否可写，是返回 trueboolean isConnectable()：&#x2F;&#x2F;是否可连接，是返回 trueboolean isAcceptable()：&#x2F;&#x2F;是否可接收，是返回 true</span><br></pre></td></tr></table></figure>

<p><strong>从SelectionKey访问Channel和Selector很简单。如下：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Channel channel &#x3D; key.channel();</span><br><span class="line">Selector selector &#x3D; key.selector();</span><br><span class="line">key.attachment();</span><br></pre></td></tr></table></figure>

<p>可以将一个对象或者更多信息附着到SelectionKey上，这样就能方便的识别某个给定的通道。例如，可以附加 与通道一起使用的Buffer，或是包含聚集数据的某个对象。使用方法如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">key.attach(theObject);</span><br><span class="line">Object attachedObj &#x3D; key.attachment();</span><br></pre></td></tr></table></figure>

<p>还可以在用register()方法向Selector注册Channel的时候附加对象。如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SelectionKey key &#x3D; channel.register(selector, SelectionKey.OP_READ, theObject);</span><br></pre></td></tr></table></figure>

<h3 id="4-从Selector中选择channel-Selecting-Channels-via-a-Selector"><a href="#4-从Selector中选择channel-Selecting-Channels-via-a-Selector" class="headerlink" title="4. 从Selector中选择channel(Selecting Channels via a Selector)"></a><strong>4. 从Selector中选择channel(Selecting Channels via a Selector)</strong></h3><p>选择器维护注册过的通道的集合，并且这种注册关系都被封装在SelectionKey当中.</p>
<blockquote>
<p>Selector维护的三种类型SelectionKey集合：</p>
</blockquote>
<ul>
<li><p><strong>已注册的键的集合(Registered key set)</strong></p>
<p>  所有与选择器关联的通道所生成的键的集合称为已经注册的键的集合。并不是所有注册过的键都仍然有效。这个集合通过 <strong>keys()</strong> 方法返回，并且可能是空的。这个已注册的键的集合不是可以直接修改的；试图这么做的话将引发java.lang.UnsupportedOperationException。</p>
</li>
<li><p><strong>已选择的键的集合(Selected key set)</strong></p>
<p>  所有与选择器关联的通道所生成的键的集合称为已经注册的键的集合。并不是所有注册过的键都仍然有效。这个集合通过 <strong>keys()</strong> 方法返回，并且可能是空的。这个已注册的键的集合不是可以直接修改的；试图这么做的话将引发java.lang.UnsupportedOperationException。</p>
</li>
<li><p><strong>已取消的键的集合(Cancelled key set)</strong></p>
<p>  已注册的键的集合的子集，这个集合包含了 <strong>cancel()</strong> 方法被调用过的键(这个键已经被无效化)，但它们还没有被注销。这个集合是选择器对象的私有成员，因而无法直接访问。</p>
<p>  <strong>注意：</strong> 当键被取消（ 可以通过<strong>isValid( )</strong> 方法来判断）时，它将被放在相关的选择器的已取消的键的集合里。注册不会立即被取消，但键会立即失效。当再次调用 <strong>select( )</strong> 方法时（或者一个正在进行的select()调用结束时），已取消的键的集合中的被取消的键将被清理掉，并且相应的注销也将完成。通道会被注销，而新的SelectionKey将被返回。当通道关闭时，所有相关的键会自动取消（记住，一个通道可以被注册到多个选择器上）。当选择器关闭时，所有被注册到该选择器的通道都将被注销，并且相关的键将立即被无效化（取消）。一旦键被无效化，调用它的与选择相关的方法就将抛出CancelledKeyException。</p>
</li>
</ul>
<blockquote>
<p>select()方法介绍：</p>
</blockquote>
<p>在刚初始化的Selector对象中，这三个集合都是空的。 <strong>通过Selector的select（）方法可以选择已经准备就绪的通道</strong> （这些通道包含你感兴趣的的事件）。比如你对读就绪的通道感兴趣，那么select（）方法就会返回读事件已经就绪的那些通道。下面是Selector几个重载的select()方法：</p>
<ul>
<li>int select()：阻塞到至少有一个通道在你注册的事件上就绪了。</li>
<li>int select(long timeout)：和select()一样，但最长阻塞时间为timeout毫秒。</li>
<li>int selectNow()：非阻塞，只要有通道就绪就立刻返回。</li>
</ul>
<p><strong>select()方法返回的int值表示有多少通道已经就绪,是自上次调用select()方法后有多少通道变成就绪状态。之前在select（）调用时进入就绪的通道不会在本次调用中被记入，而在前一次select（）调用进入就绪但现在已经不在处于就绪的通道也不会被记入</strong>。例如：首次调用select()方法，如果有一个通道变成就绪状态，返回了1，若再次调用select()方法，如果另一个通道就绪了，它会再次返回1。如果对第一个就绪的channel没有做任何操作，现在就有两个就绪的通道，但在每次select()方法调用之间，只有一个通道就绪了。</p>
<p><strong>一旦调用select()方法，并且返回值不为0时，则 可以通过调用Selector的selectedKeys()方法来访问已选择键集合</strong> 。如下：  Set selectedKeys=selector.selectedKeys();  进而可以放到和某SelectionKey关联的Selector和Channel。如下所示：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Set selectedKeys &#x3D; selector.selectedKeys();</span><br><span class="line">Iterator keyIterator &#x3D; selectedKeys.iterator();</span><br><span class="line">while(keyIterator.hasNext()) &#123;</span><br><span class="line">    SelectionKey key &#x3D; keyIterator.next();</span><br><span class="line">    if(key.isAcceptable()) &#123;</span><br><span class="line">        &#x2F;&#x2F; a connection was accepted by a ServerSocketChannel.</span><br><span class="line">    &#125; else if (key.isConnectable()) &#123;</span><br><span class="line">        &#x2F;&#x2F; a connection was established with a remote server.</span><br><span class="line">    &#125; else if (key.isReadable()) &#123;</span><br><span class="line">        &#x2F;&#x2F; a channel is ready for reading</span><br><span class="line">    &#125; else if (key.isWritable()) &#123;</span><br><span class="line">        &#x2F;&#x2F; a channel is ready for writing</span><br><span class="line">    &#125;</span><br><span class="line">    keyIterator.remove();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-停止选择的方法"><a href="#5-停止选择的方法" class="headerlink" title="5. 停止选择的方法"></a><strong>5. 停止选择的方法</strong></h3><p>选择器执行选择的过程，系统底层会依次询问每个通道是否已经就绪，这个过程可能会造成调用线程进入阻塞状态,那么我们有以下三种方式可以唤醒在select（）方法中阻塞的线程。</p>
<ul>
<li><strong>wakeup()方法</strong> ：通过调用Selector对象的wakeup（）方法让处在阻塞状态的select()方法立刻返回 该方法使得选择器上的第一个还没有返回的选择操作立即返回。如果当前没有进行中的选择操作，那么下一次对select()方法的一次调用将立即返回。</li>
<li><strong>close()方法</strong> ：通过close（）方法关闭Selector， 该方法使得任何一个在选择操作中阻塞的线程都被唤醒（类似wakeup（）），同时使得注册到该Selector的所有Channel被注销，所有的键将被取消，但是Channel本身并不会关闭。</li>
</ul>
<h2 id="三-模板代码"><a href="#三-模板代码" class="headerlink" title="三 模板代码"></a><strong>三 模板代码</strong></h2><p><strong>一个服务端的模板代码：</strong></p>
<p>有了模板代码我们在编写程序时，大多数时间都是在模板代码中添加相应的业务代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">ServerSocketChannel ssc &#x3D; ServerSocketChannel.open();</span><br><span class="line">ssc.socket().bind(new InetSocketAddress(&quot;localhost&quot;, 8080));</span><br><span class="line">ssc.configureBlocking(false);</span><br><span class="line"></span><br><span class="line">Selector selector &#x3D; Selector.open();</span><br><span class="line">ssc.register(selector, SelectionKey.OP_ACCEPT);</span><br><span class="line"></span><br><span class="line">while(true) &#123;</span><br><span class="line">    int readyNum &#x3D; selector.select();</span><br><span class="line">    if (readyNum &#x3D;&#x3D; 0) &#123;</span><br><span class="line">        continue;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    Set&lt;SelectionKey&gt; selectedKeys &#x3D; selector.selectedKeys();</span><br><span class="line">    Iterator&lt;SelectionKey&gt; it &#x3D; selectedKeys.iterator();</span><br><span class="line"></span><br><span class="line">    while(it.hasNext()) &#123;</span><br><span class="line">        SelectionKey key &#x3D; it.next();</span><br><span class="line"></span><br><span class="line">        if(key.isAcceptable()) &#123;</span><br><span class="line">            &#x2F;&#x2F; 接受连接</span><br><span class="line">        &#125; else if (key.isReadable()) &#123;</span><br><span class="line">            &#x2F;&#x2F; 通道可读</span><br><span class="line">        &#125; else if (key.isWritable()) &#123;</span><br><span class="line">            &#x2F;&#x2F; 通道可写</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        it.remove();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四-客户端与服务端简单交互实例"><a href="#四-客户端与服务端简单交互实例" class="headerlink" title="四 客户端与服务端简单交互实例"></a><strong>四 客户端与服务端简单交互实例</strong></h2><p><strong>服务端：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">package selector;</span><br><span class="line"></span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.net.InetSocketAddress;</span><br><span class="line">import java.nio.ByteBuffer;</span><br><span class="line">import java.nio.channels.SelectionKey;</span><br><span class="line">import java.nio.channels.Selector;</span><br><span class="line">import java.nio.channels.ServerSocketChannel;</span><br><span class="line">import java.nio.channels.SocketChannel;</span><br><span class="line">import java.util.Iterator;</span><br><span class="line">import java.util.Set;</span><br><span class="line"></span><br><span class="line">public class WebServer &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            ServerSocketChannel ssc &#x3D; ServerSocketChannel.open();</span><br><span class="line">            ssc.socket().bind(new InetSocketAddress(&quot;127.0.0.1&quot;, 8000));</span><br><span class="line">            ssc.configureBlocking(false);</span><br><span class="line"></span><br><span class="line">            Selector selector &#x3D; Selector.open();</span><br><span class="line">            &#x2F;&#x2F; 注册 channel，并且指定感兴趣的事件是 Accept</span><br><span class="line">            ssc.register(selector, SelectionKey.OP_ACCEPT);</span><br><span class="line"></span><br><span class="line">            ByteBuffer readBuff &#x3D; ByteBuffer.allocate(1024);</span><br><span class="line">            ByteBuffer writeBuff &#x3D; ByteBuffer.allocate(128);</span><br><span class="line">            writeBuff.put(&quot;received&quot;.getBytes());</span><br><span class="line">            writeBuff.flip();</span><br><span class="line"></span><br><span class="line">            while (true) &#123;</span><br><span class="line">                int nReady &#x3D; selector.select();</span><br><span class="line">                Set&lt;SelectionKey&gt; keys &#x3D; selector.selectedKeys();</span><br><span class="line">                Iterator&lt;SelectionKey&gt; it &#x3D; keys.iterator();</span><br><span class="line"></span><br><span class="line">                while (it.hasNext()) &#123;</span><br><span class="line">                    SelectionKey key &#x3D; it.next();</span><br><span class="line">                    it.remove();</span><br><span class="line"></span><br><span class="line">                    if (key.isAcceptable()) &#123;</span><br><span class="line">                        &#x2F;&#x2F; 创建新的连接，并且把连接注册到selector上，而且，&#x2F;&#x2F; 声明这个channel只对读操作感兴趣。</span><br><span class="line">                        SocketChannel socketChannel &#x3D; ssc.accept();</span><br><span class="line">                        socketChannel.configureBlocking(false);</span><br><span class="line">                        socketChannel.register(selector, SelectionKey.OP_READ);</span><br><span class="line">                    &#125;</span><br><span class="line">                    else if (key.isReadable()) &#123;</span><br><span class="line">                        SocketChannel socketChannel &#x3D; (SocketChannel) key.channel();</span><br><span class="line">                        readBuff.clear();</span><br><span class="line">                        socketChannel.read(readBuff);</span><br><span class="line"></span><br><span class="line">                        readBuff.flip();</span><br><span class="line">                        System.out.println(&quot;received : &quot; + new String(readBuff.array()));</span><br><span class="line">                        key.interestOps(SelectionKey.OP_WRITE);</span><br><span class="line">                    &#125;</span><br><span class="line">                    else if (key.isWritable()) &#123;</span><br><span class="line">                        writeBuff.rewind();</span><br><span class="line">                        SocketChannel socketChannel &#x3D; (SocketChannel) key.channel();</span><br><span class="line">                        socketChannel.write(writeBuff);</span><br><span class="line">                        key.interestOps(SelectionKey.OP_READ);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>客户端：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">package selector;</span><br><span class="line"></span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.net.InetSocketAddress;</span><br><span class="line">import java.nio.ByteBuffer;</span><br><span class="line">import java.nio.channels.SocketChannel;</span><br><span class="line"></span><br><span class="line">public class WebClient &#123;</span><br><span class="line">    public static void main(String[] args) throws IOException &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            SocketChannel socketChannel &#x3D; SocketChannel.open();</span><br><span class="line">            socketChannel.connect(new InetSocketAddress(&quot;127.0.0.1&quot;, 8000));</span><br><span class="line"></span><br><span class="line">            ByteBuffer writeBuffer &#x3D; ByteBuffer.allocate(32);</span><br><span class="line">            ByteBuffer readBuffer &#x3D; ByteBuffer.allocate(32);</span><br><span class="line"></span><br><span class="line">            writeBuffer.put(&quot;hello&quot;.getBytes());</span><br><span class="line">            writeBuffer.flip();</span><br><span class="line"></span><br><span class="line">            while (true) &#123;</span><br><span class="line">                writeBuffer.rewind();</span><br><span class="line">                socketChannel.write(writeBuffer);</span><br><span class="line">                readBuffer.clear();</span><br><span class="line">                socketChannel.read(readBuffer);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>运行结果：</strong></p>
<p>先运行服务端，再运行客户端，服务端会不断收到客户端发送过来的消息。</p>
<p><a target="_blank" rel="noopener" href="https://user-gold-cdn.xitu.io/2018/5/16/1636720b53ff3a72?w=1090&h=217&f=png&s=15376">https://user-gold-cdn.xitu.io/2018/5/16/1636720b53ff3a72?w=1090&amp;h=217&amp;f=png&amp;s=15376</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B61%EF%BC%9ASocket%E5%92%8CServerSocket%E5%85%A5%E9%97%A8%E5%AE%9E%E6%88%98%EF%BC%8C%E5%AE%9E%E7%8E%B0%E5%8D%95%E8%81%8A/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B61%EF%BC%9ASocket%E5%92%8CServerSocket%E5%85%A5%E9%97%A8%E5%AE%9E%E6%88%98%EF%BC%8C%E5%AE%9E%E7%8E%B0%E5%8D%95%E8%81%8A/" class="post-title-link" itemprop="url">手动搭建I/O网络通信框架1：Socket和ServerSocket入门实战，实现单聊</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-05-28 19:39:32 / 修改时间：09:40:48" itemprop="dateCreated datePublished" datetime="2021-05-28T19:39:32+08:00">2021-05-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/Netty/" itemprop="url" rel="index"><span itemprop="name">Netty</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="手动搭建I-O网络通信框架1：Socket和ServerSocket入门实战，实现单聊"><a href="#手动搭建I-O网络通信框架1：Socket和ServerSocket入门实战，实现单聊" class="headerlink" title="手动搭建I/O网络通信框架1：Socket和ServerSocket入门实战，实现单聊"></a>手动搭建I/O网络通信框架1：Socket和ServerSocket入门实战，实现单聊</h1><blockquote>
<p>转载自<a target="_blank" rel="noopener" href="https://www.cnblogs.com/lbhym/p/12673470.html">https://www.cnblogs.com/lbhym/p/12673470.html</a></p>
</blockquote>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>这个基础项目会作为BIO、NIO、AIO的一个前提，后面会有数篇博客会基于这个小项目利用BIO、NIO、AIO进行改造升级。</p>
<p>简单的说一下io，了解的直接跳过看代码吧:IO常见的使用场景就是网络通信或读取文件等方面。IO流分为字节流和字符流。字节即Byte，包含八位二进制数，一个二进制数就是1bit，中文名称叫位。字符即一个字母或者一个汉字。一个字母由一个字节组成，而汉字根据编码不同由2个或者3个组成。Java.io包如下:详细的API可自行查阅资料</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200410142107890-242008210.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200410142107890-242008210.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200410142126015-790268014.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200410142126015-790268014.png"></p>
<p><strong>Socket定义</strong>：套接字（socket）是一个抽象层，应用程序可以通过它发送或接收数据，可对其进行像对文件一样的打开、读写和关闭等操作。套接字允许应用程序将I/O插入到网络中，并与网络中的其他应用程序进行通信。网络套接字是IP地址与端口的组合。</p>
<p><strong>可以理解为两台机器或进程间进行网络通信的端点，这个端点包含IP地址和端口号。</strong></p>
<p>Socket和ServerSocket区别就如其名字一样，简单地说ServerSocket作用在服务端，用以监听客户端的请求。Socket作用在客户端和服务端，用以发送接收消息。但是就像上面说的，它们都要包含一个IP地址和端口号。</p>
<h1 id="Socket和ServerSocket实战："><a href="#Socket和ServerSocket实战：" class="headerlink" title="Socket和ServerSocket实战："></a><strong>Socket和ServerSocket实战：</strong></h1><p>首先创建一个最普通的Java项目。然后创建两个类，Server和Client。其代码和注释如下,仔细看下注释和代码，还是比较简单的</p>
<p>服务器只能为一个客户端服务，一旦监听到客户端的请求，就会一直被这个客户端占用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Client</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//这是服务端的IP和端口</span></span><br><span class="line">        <span class="keyword">final</span> String DEFAULT_SERVER_HOST = <span class="string">&quot;127.0.0.1&quot;</span>;</span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">int</span> DEFAULT_SERVER_PORT = <span class="number">8888</span>;</span><br><span class="line">        <span class="comment">//创建Socket</span></span><br><span class="line">        <span class="keyword">try</span> (Socket socket = <span class="keyword">new</span> Socket(DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT)) &#123;</span><br><span class="line">            <span class="comment">//接收消息</span></span><br><span class="line">            BufferedReader reader = <span class="keyword">new</span> BufferedReader(</span><br><span class="line">                    <span class="keyword">new</span> InputStreamReader(socket.getInputStream())</span><br><span class="line">            );</span><br><span class="line">            <span class="comment">//发送消息</span></span><br><span class="line">            BufferedWriter writer = <span class="keyword">new</span> BufferedWriter(</span><br><span class="line">                    <span class="keyword">new</span> OutputStreamWriter(socket.getOutputStream())</span><br><span class="line">            );</span><br><span class="line">            <span class="comment">//获取用户输入的消息</span></span><br><span class="line">            BufferedReader userReader = <span class="keyword">new</span> BufferedReader(</span><br><span class="line">                    <span class="keyword">new</span> InputStreamReader(System.in)</span><br><span class="line">            );</span><br><span class="line">            String msg = <span class="keyword">null</span>;</span><br><span class="line">            <span class="comment">//循环的话客户端就可以一直输入消息，不然执行完try catch会自动释放资源，也就是断开连接</span></span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                String input = userReader.readLine();</span><br><span class="line">                <span class="comment">//写入客户端要发送的消息。因为服务端用readLine获取消息，其以\n为终点，所以要在消息最后加上\n</span></span><br><span class="line">                writer.write(input + <span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                writer.flush();</span><br><span class="line">                msg = reader.readLine();</span><br><span class="line">                System.out.println(msg);</span><br><span class="line">                <span class="comment">//如果客户端输入quit就可以跳出循环、断开连接了</span></span><br><span class="line">                <span class="keyword">if</span>(input.equals(<span class="string">&quot;quit&quot;</span>))&#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Server</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">int</span> DEFAULT_PORT = <span class="number">8888</span>;</span><br><span class="line">        <span class="comment">//创建ServerSocket监听8888端口</span></span><br><span class="line">        <span class="keyword">try</span> (ServerSocket serverSocket = <span class="keyword">new</span> ServerSocket(DEFAULT_PORT)) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;ServerSocket Start,The Port is:&quot;</span> + DEFAULT_PORT);</span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;<span class="comment">//不停地监听该端口</span></span><br><span class="line">                <span class="comment">//阻塞式的监听，如果没有客户端请求就一直停留在这里</span></span><br><span class="line">                Socket socket = serverSocket.accept();</span><br><span class="line">                System.out.println(<span class="string">&quot;Client[&quot;</span> + socket.getPort() + <span class="string">&quot;]Online&quot;</span>);</span><br><span class="line">                <span class="comment">//接收消息</span></span><br><span class="line">                BufferedReader reader = <span class="keyword">new</span> BufferedReader(</span><br><span class="line">                        <span class="keyword">new</span> InputStreamReader(socket.getInputStream())</span><br><span class="line">                );</span><br><span class="line">                <span class="comment">//发送消息</span></span><br><span class="line">                BufferedWriter writer = <span class="keyword">new</span> BufferedWriter(</span><br><span class="line">                        <span class="keyword">new</span> OutputStreamWriter(socket.getOutputStream())</span><br><span class="line">                );</span><br><span class="line"></span><br><span class="line">                String msg = <span class="keyword">null</span>;</span><br><span class="line">                <span class="keyword">while</span> ((msg = reader.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">                    System.out.println(<span class="string">&quot;Client[&quot;</span> + socket.getPort() + <span class="string">&quot;]:&quot;</span> + msg);</span><br><span class="line">                    <span class="comment">//写入服务端要发送的消息</span></span><br><span class="line">                    writer.write(<span class="string">&quot;Server:&quot;</span> + msg + <span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                    writer.flush();</span><br><span class="line">                    <span class="comment">//如果客户端的消息是quit代表他退出了，并跳出循环，不用再接收他的消息了。如果客户端再次连接就会重新上线</span></span><br><span class="line">                    <span class="keyword">if</span> (msg.equals(<span class="string">&quot;quit&quot;</span>)) &#123;</span><br><span class="line">                        System.out.println(<span class="string">&quot;Client[&quot;</span> + socket.getPort() + <span class="string">&quot;]:Offline&quot;</span>);</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后打开两个命令终端，通过javac编译后，一个运行Server代表服务器，一个运行Client代表客户端。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200410144802569-1725038127.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200410144802569-1725038127.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200410144811290-2056832827.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200410144811290-2056832827.png"></p>
<p>下一篇 <a href="%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B62%EF%BC%9ABIO%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%E5%AE%9E%E7%8E%B0%E7%BE%A4%E8%81%8A%202d2b7fd177844b1ab85c0276e7ae1e7b.md">手动搭建I/O网络通信框架2：BIO编程模型实现群聊</a>  。</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B62%EF%BC%9ABIO%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%E5%AE%9E%E7%8E%B0%E7%BE%A4%E8%81%8A/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B62%EF%BC%9ABIO%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%E5%AE%9E%E7%8E%B0%E7%BE%A4%E8%81%8A/" class="post-title-link" itemprop="url">手动搭建I/O网络通信框架2：BIO编程模型实现群聊</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-05-28 19:39:32 / 修改时间：09:40:58" itemprop="dateCreated datePublished" datetime="2021-05-28T19:39:32+08:00">2021-05-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/Netty/" itemprop="url" rel="index"><span itemprop="name">Netty</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="手动搭建I-O网络通信框架2：BIO编程模型实现群聊"><a href="#手动搭建I-O网络通信框架2：BIO编程模型实现群聊" class="headerlink" title="手动搭建I/O网络通信框架2：BIO编程模型实现群聊"></a>手动搭建I/O网络通信框架2：BIO编程模型实现群聊</h1><blockquote>
<p>转载自<a target="_blank" rel="noopener" href="https://www.cnblogs.com/lbhym/p/12681787.html">https://www.cnblogs.com/lbhym/p/12681787.html</a></p>
</blockquote>
<h1 id="手动搭建I-O网络通信框架2：BIO编程模型实现群聊-1"><a href="#手动搭建I-O网络通信框架2：BIO编程模型实现群聊-1" class="headerlink" title="手动搭建I/O网络通信框架2：BIO编程模型实现群聊"></a><strong>手动搭建I/O网络通信框架2：BIO编程模型实现群聊</strong></h1><p>在第一章中运用Socket和ServerSocket简单的实现了网络通信。这一章，利用BIO编程模型进行升级改造，实现群聊聊天室。</p>
<p>所谓BIO，就是Block IO，阻塞式的IO。这个阻塞主要发生在：ServerSocket接收请求时（accept()方法）、InputStream、OutputStream（输入输出流的读和写）都是阻塞的。这个可以在下面代码的调试中发现，比如在客户端接收服务器消息的输入流处打上断点，除非服务器发来消息，不然断点是一直停在这个地方的。也就是说这个线程在这时间是被阻塞的。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200411190359629-713089288.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200411190359629-713089288.png"></p>
<p>如图：当一个客户端请求进来时，接收器会为这个客户端分配一个工作线程，这个工作线程专职处理客户端的操作。在上一章中，服务器接收到客户端请求后就跑去专门服务这个客户端了，所以当其他请求进来时，是处理不到的。</p>
<p>看到这个图，很容易就会想到线程池，BIO是一个相对简单的模型，实现它的关键之处也在于线程池。</p>
<p>在上代码之前，先大概说清楚每个类的作用，以免弄混淆。更详细的说明，都写在注释当中。</p>
<h2 id="服务器端："><a href="#服务器端：" class="headerlink" title="服务器端："></a><strong>服务器端：</strong></h2><p>ChatServer:这个类的作用就像图中的Acceptor。它有两个比较关键的全局变量，一个就是存储在线用户信息的Map，一个就是线程池。这个类会监听端口，接收客户端的请求，然后为客户端分配工作线程。还会提供一些常用的工具方法给每个工作线程调用，比如：发送消息、添加在线用户等。我之前简单用过Netty和WebSocket，这个类看上去就已经和这些框架有点相似了。学习IO编程模型也是为了接下来深入学习Netty做准备。</p>
<p>ChatHandler:这个类就是工作线程的类。在这个项目中，它的工作很简单：把接收到的消息转发给其他客户端，当然还有一些小功能，比如添加\移除在线用户。</p>
<h2 id="客户端："><a href="#客户端：" class="headerlink" title="客户端："></a><strong>客户端：</strong></h2><p>相较于服务器，客户端的改动较小，主要是把等待用户输入信息这个功能分到其他线程做，不然这个功能会一直阻塞主线程，导致无法接收其他客户端的消息。</p>
<p>ChatClient:客户端启动类，也就是主线程，会通过Socket和服务器连接。也提供了两个工具方法：发送消息和接收消息。</p>
<p>UserInputHandler:专门负责等待用户输入信息的线程，一旦有信息键入，就马上发送给服务器。</p>
<p>首先创建两个包区分一下客户端和服务器，client和server</p>
<p><strong>服务器端ChatServer：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatServer</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> DEFAULT_PORT = <span class="number">8888</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建一个Map存储在线用户的信息。这个map可以统计在线用户、针对这些用户可以转发其他用户发送的消息</span></span><br><span class="line"><span class="comment">     * 因为会有多个线程操作这个map，所以为了安全起见用ConcurrentHashMap</span></span><br><span class="line"><span class="comment">     * 在这里key就是客户端的端口号，但在实际中肯定不会用端口号区分用户，如果是web的话一般用session。</span></span><br><span class="line"><span class="comment">     * value是IO的Writer，用以存储客户端发送的消息</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Map&lt;Integer, Writer&gt; map=<span class="keyword">new</span> ConcurrentHashMap&lt;&gt;();</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建线程池，线程上限为10个，如果第11个客户端请求进来，服务器会接收但是不会去分配线程处理它。</span></span><br><span class="line"><span class="comment">     * 前10个客户端的聊天记录，它看不见。当有一个客户端下线时，这第11个客户端就会被分配线程，服务器显示在线</span></span><br><span class="line"><span class="comment">     * 大家可以把10再设置小一点，测试看看</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> ExecutorService executorService= Executors.newFixedThreadPool(<span class="number">10</span>);</span><br><span class="line">    <span class="comment">//客户端连接时往map添加客户端</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addClient</span><span class="params">(Socket socket)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (socket != <span class="keyword">null</span>) &#123;</span><br><span class="line">            BufferedWriter writer = <span class="keyword">new</span> BufferedWriter(</span><br><span class="line">                    <span class="keyword">new</span> OutputStreamWriter(socket.getOutputStream())</span><br><span class="line">            );</span><br><span class="line">            map.put(socket.getPort(), writer);</span><br><span class="line">            System.out.println(<span class="string">&quot;Client[&quot;</span>+socket.getPort()+<span class="string">&quot;]:Online&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//断开连接时map里移除客户端</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">removeClient</span><span class="params">(Socket socket)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (socket != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (map.containsKey(socket.getPort())) &#123;</span><br><span class="line">                map.get(socket.getPort()).close();</span><br><span class="line">                map.remove(socket.getPort());</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;Client[&quot;</span> + socket.getPort() + <span class="string">&quot;]Offline&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//转发客户端消息，这个方法就是把消息发送给在线的其他的所有客户端</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sendMessage</span><span class="params">(Socket socket, String msg)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">//遍历在线客户端</span></span><br><span class="line">        <span class="keyword">for</span> (Integer port : map.keySet()) &#123;</span><br><span class="line">            <span class="comment">//发送给在线的其他客户端</span></span><br><span class="line">            <span class="keyword">if</span> (port != socket.getPort()) &#123;</span><br><span class="line">                Writer writer = map.get(port);</span><br><span class="line">                writer.write(msg);</span><br><span class="line">                writer.flush();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//接收客户端请求，并分配Handler去处理请求</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> (ServerSocket serverSocket = <span class="keyword">new</span> ServerSocket(DEFAULT_PORT)) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;Server Start,The Port is:&quot;</span>+DEFAULT_PORT);</span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">                <span class="comment">//等待客户端连接</span></span><br><span class="line">                Socket socket=serverSocket.accept();</span><br><span class="line">                <span class="comment">//为客户端分配一个ChatHandler线程</span></span><br><span class="line">                executorService.execute(<span class="keyword">new</span> ChatHandler(<span class="keyword">this</span>,socket));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ChatServer server=<span class="keyword">new</span> ChatServer();</span><br><span class="line">        server.start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>服务器端ChatHandler：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatHandler</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> ChatServer server;</span><br><span class="line">    <span class="keyword">private</span> Socket socket;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//构造函数，ChatServer通过这个分配Handler线程</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ChatHandler</span><span class="params">(ChatServer server, Socket socket)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.server = server;</span><br><span class="line">        <span class="keyword">this</span>.socket = socket;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//往map里添加这个客户端</span></span><br><span class="line">            server.addClient(socket);</span><br><span class="line">            <span class="comment">//读取这个客户端发送的消息</span></span><br><span class="line">            BufferedReader reader = <span class="keyword">new</span> BufferedReader(</span><br><span class="line">                    <span class="keyword">new</span> InputStreamReader(socket.getInputStream())</span><br><span class="line">            );</span><br><span class="line">            String msg = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">while</span> ((msg = reader.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="comment">//这样拼接是为了让其他客户端也能看清是谁发送的消息</span></span><br><span class="line">                String sendmsg = <span class="string">&quot;Client[&quot;</span> + socket.getPort() + <span class="string">&quot;]:&quot;</span> + msg;</span><br><span class="line">                <span class="comment">//服务器打印这个消息</span></span><br><span class="line">                System.out.println(sendmsg);</span><br><span class="line">                <span class="comment">//将收到的消息转发给其他在线客户端</span></span><br><span class="line">                server.sendMessage(socket, sendmsg + <span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                <span class="keyword">if</span> (msg.equals(<span class="string">&quot;quit&quot;</span>)) &#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="comment">//如果用户退出或者发生异常，就在map中移除该客户端</span></span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                server.removeClient(socket);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>客户端ChatClient:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatClient</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> BufferedReader reader;</span><br><span class="line">    <span class="keyword">private</span> BufferedWriter writer;</span><br><span class="line">    <span class="keyword">private</span> Socket socket;</span><br><span class="line">    <span class="comment">//发送消息给服务器</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sendToServer</span><span class="params">(String msg)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">//发送之前，判断socket的输出流是否关闭</span></span><br><span class="line">        <span class="keyword">if</span> (!socket.isOutputShutdown()) &#123;</span><br><span class="line">            <span class="comment">//如果没有关闭就把用户键入的消息放到writer里面</span></span><br><span class="line">            writer.write(msg + <span class="string">&quot;\n&quot;</span>);</span><br><span class="line">            writer.flush();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//从服务器接收消息</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">receive</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        String msg = <span class="keyword">null</span>;</span><br><span class="line">        <span class="comment">//判断socket的输入流是否关闭</span></span><br><span class="line">        <span class="keyword">if</span> (!socket.isInputShutdown()) &#123;</span><br><span class="line">            <span class="comment">//没有关闭的话就可以通过reader读取服务器发送来的消息。注意：如果没有读取到消息线程会阻塞在这里</span></span><br><span class="line">            msg = reader.readLine();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> msg;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//和服务创建连接</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            socket = <span class="keyword">new</span> Socket(<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>);</span><br><span class="line">            reader=<span class="keyword">new</span> BufferedReader(</span><br><span class="line">                    <span class="keyword">new</span> InputStreamReader(socket.getInputStream())</span><br><span class="line">            );</span><br><span class="line">            writer=<span class="keyword">new</span> BufferedWriter(</span><br><span class="line">                    <span class="keyword">new</span> OutputStreamWriter(socket.getOutputStream())</span><br><span class="line">            );</span><br><span class="line">            <span class="comment">//新建一个线程去监听用户输入的消息</span></span><br><span class="line">            <span class="keyword">new</span> Thread(<span class="keyword">new</span> UserInputHandler(<span class="keyword">this</span>)).start();</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * 不停的读取服务器转发的其他客户端的信息</span></span><br><span class="line"><span class="comment">             * 记录一下之前踩过的小坑：</span></span><br><span class="line"><span class="comment">             * 这里一定要创建一个msg接收信息，如果直接用receive()方法判断和输出receive()的话会造成有的消息不会显示</span></span><br><span class="line"><span class="comment">             * 因为receive()获取时，在返回之前是阻塞的，一旦接收到消息才会返回，也就是while这里是阻塞的，一旦有消息就会进入到while里面</span></span><br><span class="line"><span class="comment">             * 这时候如果输出的是receive(),那么上次获取的信息就会丢失，然后阻塞在System.out.println</span></span><br><span class="line"><span class="comment">             * */</span></span><br><span class="line">            String msg=<span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">while</span> ((msg=receive())!=<span class="keyword">null</span>)&#123;</span><br><span class="line">                System.out.println(msg);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">               <span class="keyword">if</span>(writer!=<span class="keyword">null</span>)&#123;</span><br><span class="line">                   writer.close();</span><br><span class="line">               &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> ChatClient().start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>　<strong>客户端UserInputHandler:</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserInputHandler</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> ChatClient client;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">UserInputHandler</span><span class="params">(ChatClient client)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.client = client;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//接收用户输入的消息</span></span><br><span class="line">            BufferedReader reader = <span class="keyword">new</span> BufferedReader(</span><br><span class="line">                    <span class="keyword">new</span> InputStreamReader(System.in)</span><br><span class="line">            );</span><br><span class="line">            <span class="comment">//不停的获取reader中的System.in，实现了等待用户输入的效果</span></span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                String input = reader.readLine();</span><br><span class="line">                <span class="comment">//向服务器发送消息</span></span><br><span class="line">                client.sendToServer(input);</span><br><span class="line">                <span class="keyword">if</span> (input.equals(<span class="string">&quot;quit&quot;</span>))</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="运行测试："><a href="#运行测试：" class="headerlink" title="运行测试："></a><strong>运行测试：</strong></h2><p>通过打开终端，通过javac编译。如果大家是在IDEA上编码的话可能会报编码错误，在javac后面加上-encoding utf-8再接java文件就好了。</p>
<p>编译后运行，通过java运行时，又遇到了一个坑。会报找不到主类的错误，原来是因为加上两个包，要在class文件名前面加上包名。比如当前在src目录，下面有client和server两个包，要这么运行：java client.XXXX。可我之前明明在client文件夹下运行的java，也是不行，不知道为什么。</p>
<p>接着测试：</p>
<p>1.首先在一个终端里运行ChatServer，打开服务器</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200411193234531-59946096.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200411193234531-59946096.png"></p>
<p>2.在第二个终端里打开ChatClient，暂且叫A，此时服务器的终端显示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200411193327017-1431424368.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200411193327017-1431424368.png"></p>
<p>3.类似的，在第三个终端里打开ChatClient，暂且叫B，此时服务器显示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200411193358577-1779741867.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200411193358577-1779741867.png"></p>
<p>4.A中输入hi,除了服务器会打印hi外，B中也会显示，图片中的端口号和前面的不一样，是因为中间出了点小问题，前三张截图和后面的不是同时运行的。实际中同一个客户端会显示一样的端口号：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200411193747903-851197924.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200411193747903-851197924.png"></p>
<p>5.当客户端输入quit时就会断开连接，最后，服务器的显示为：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200411193923381-1365643546.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200411193923381-1365643546.png"></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B63%EF%BC%9ANIO%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%EF%BC%8C%E5%8D%87%E7%BA%A7%E6%94%B9%E9%80%A0%E8%81%8A%E5%A4%A9%E5%AE%A4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B63%EF%BC%9ANIO%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%EF%BC%8C%E5%8D%87%E7%BA%A7%E6%94%B9%E9%80%A0%E8%81%8A%E5%A4%A9%E5%AE%A4/" class="post-title-link" itemprop="url">手动搭建I/O网络通信框架3：NIO编程模型，升级改造聊天室</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-05-28 19:39:32 / 修改时间：09:45:12" itemprop="dateCreated datePublished" datetime="2021-05-28T19:39:32+08:00">2021-05-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/Netty/" itemprop="url" rel="index"><span itemprop="name">Netty</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="手动搭建I-O网络通信框架3：NIO编程模型，升级改造聊天室"><a href="#手动搭建I-O网络通信框架3：NIO编程模型，升级改造聊天室" class="headerlink" title="手动搭建I/O网络通信框架3：NIO编程模型，升级改造聊天室"></a>手动搭建I/O网络通信框架3：NIO编程模型，升级改造聊天室</h1><blockquote>
<p>转载自 <a target="_blank" rel="noopener" href="https://www.cnblogs.com/lbhym/p/12698309.html">https://www.cnblogs.com/lbhym/p/12698309.html</a></p>
</blockquote>
<h1 id="手动搭建I-O网络通信框架3：NIO编程模型，升级改造聊天室-1"><a href="#手动搭建I-O网络通信框架3：NIO编程模型，升级改造聊天室-1" class="headerlink" title="手动搭建I/O网络通信框架3：NIO编程模型，升级改造聊天室"></a><strong>手动搭建I/O网络通信框架3：NIO编程模型，升级改造聊天室</strong></h1><p>在“手动搭建I/O网络通信框架2：BIO编程模型实现群聊”中用BIO编程模型，简单的实现了一个聊天室。但是其最大的问题在解释BIO时就已经说了：ServerSocket接收请求时（accept()方法）、InputStream、OutputStream（输入输出流的读和写）都是阻塞的。还有一个问题就是线程池，线程多了，服务器性能耗不起。线程少了，在聊天室这种场景下，让用户等待连接肯定不可取。今天要说到的NIO编程模型就很好的解决了这几个问题。有两个主要的替换地方：</p>
<ol>
<li>用Channel代替Stream。</li>
<li>使用Selector监控多条Channel，起到类似线程池的作用，但是它只需一条线程。</li>
</ol>
<p>既然要用NIO编程模型，那就要说说它的三个主要核心：Selector、Channel、Buffer。它们的关系是：一个Selector管理多个Channel，一个Channel可以往Buffer中写入和读取数据。Buffer名叫缓冲区，底层其实是一个数组，会提供一些方法往数组写入读取数据。</p>
<h2 id="Buffer"><a href="#Buffer" class="headerlink" title="Buffer:"></a><strong>Buffer:</strong></h2><p>不太了解Buffer的可以看看这个：<a target="_blank" rel="noopener" href="https://blog.csdn.net/czx2018/article/details/89502699">https://blog.csdn.net/czx2018/article/details/89502699</a></p>
<p><strong>常用API：</strong></p>
<ul>
<li>allocate() - 初始化一块缓冲区</li>
<li>put() - 向缓冲区写入数据</li>
<li>get() - 向缓冲区读数据</li>
<li>filp() - 将缓冲区的读写模式转换</li>
<li>clear() - 这个并不是把缓冲区里的数据清除，而是利用后来写入的数据来覆盖原来写入的数据，以达到类似清除了老的数据的效果</li>
<li>compact() - 从读数据切换到写模式，数据不会被清空，会将所有未读的数据copy到缓冲区头部，后续写数据不会覆盖，而是在这些数据之后写数据</li>
<li>mark() - 对position做出标记，配合reset使用</li>
<li>reset() - 将position置为标记值</li>
</ul>
<p><em>简单地说</em>：Buffer实质上是个数组，有两个关键的指针，一个position代表当前数据写入到哪了、一个limit代表限制。初始化时设置了数组长度，这limit就是数组的长度。如：设置<code>intBuffer.allocate(10)</code>，最大存储10个int数据，写入5五个数据后，需要读取数据了。用filp()转换读写模式后，limit=position，position=0。也就是说从0开始读，只能读到第五个。读完后这个缓冲区就需要clear()了，实际上并没有真的去清空数据，而是position和limit两个指针又回到了初始化的位置，接着又可以写入数据了，反正数组下标相同重新写入数据会覆盖，就没必要真的去清空了。</p>
<h2 id="Channel"><a href="#Channel" class="headerlink" title="Channel:"></a><strong>Channel:</strong></h2><p>Channel(通道)主要用于传输数据，然后从Buffer中写入或读取。它们两个结合起来虽然和流有些相似，但主要有以下几点区别：　　</p>
<ol>
<li>流是单向的，可以发现Stream的输入流和输出流是独立的，它们只能输入或输出。而通道既可以读也可以写。　　</li>
<li>通道本身不能存放数据，只能借助Buffer。　　</li>
<li>Channel支持异步。　　</li>
</ol>
<p>Channel有如下三个常用的类：FileChannel、SocketChannel、ServerSocketChannel。从名字也可以看出区别，第一个是对文件数据的读写，后面两个则是针对Socket和ServerSocket，这里我们只是用后面两个。更详细的用法可以看：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/snailclimb/p/9086335.html%EF%BC%8C%E4%B8%8B%E9%9D%A2%E7%9A%84%E4%BB%A3%E7%A0%81%E4%B8%AD%E4%B9%9F%E4%BC%9A%E7%94%A8%E5%88%B0%EF%BC%8C%E4%BC%9A%E6%9C%89%E8%AF%A6%E7%BB%86%E7%9A%84%E6%B3%A8%E9%87%8A%E3%80%82">https://www.cnblogs.com/snailclimb/p/9086335.html，下面的代码中也会用到，会有详细的注释。</a></p>
<h2 id="Selector"><a href="#Selector" class="headerlink" title="Selector"></a><strong>Selector</strong></h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414150122707-1475260423.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414150122707-1475260423.png"></p>
<p>多个Channel可以注册到Selector，就可以直接通过一个Selector管理多个通道。Channel在不同的时间或者不同的事件下有不同的状态，Selector会通过轮询来达到监视的效果，如果查到Channel的状态正好是我们注册时声明的所要监视的状态，我们就可以查出这些通道，然后做相应的处理。这些状态如下：　　1.客户端的SocketChannel和服务器端建立连接，SocketChannel状态就是<strong>Connect</strong>。　　2.服务器端的ServerSocketChannel接收了客户端的请求，ServerSocketChannel状态就是<strong>Accept</strong>。　　3.当SocketChannel有数据可读，那么它们的状态就是<strong>Read</strong>。　　4.当我们需要向Channel中写数据时，那么它们的状态就是<strong>Write</strong>。　　</p>
<p>具体的使用见下面代码注释或看 <a href="Java%20NIO%E4%B9%8BSelector%EF%BC%88%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%89%20608ea119b60443d6a6233f32004d92ec.md">Java NIO之Selector（选择器）</a> </p>
<h2 id="NIO编程模型"><a href="#NIO编程模型" class="headerlink" title="NIO编程模型"></a><strong>NIO编程模型</strong></h2><p>NIO编程模型工作流程：　　</p>
<ol>
<li>首先会创建一个Selector，用来监视管理各个不同的Channel，也就是不同的客户端。相当于取代了原来BIO的线程池，但是它只需一个线程就可以处理多个Channel，没有了线程上下文切换带来的消耗，很好的优化了性能。　　</li>
<li>创建一个ServerSocketChannel监听通信端口，并注册到Selector，让Seletor监视这个通道的Accept状态，也就是接收客户端请求的状态。</li>
<li>此时客户端ClientA请求服务器，那么Selector就知道了有客户端请求进来。这时候我们可以得到客户端的SocketChannel，并为这个通道注册Read状态，也就是Selector会监听ClientA发来的消息。　　</li>
<li>一旦接收到ClientA的消息，就会用其他客户端的SocketChannel的Write状态，向它们转发ClientA的消息。</li>
</ol>
<p>上代码之前，还是先说说各个类的作用：</p>
<p>相比较BIO的代码，NIO的代码还少了一个类，那就是服务器端的工作线程类。没了线程池，自然也不需要一个单独的线程去服务客户端。客户端还是需要一个单独的线程去等待用户输入，因为用户随时都可能输入信息，这个没法预见，只能阻塞式的等待。</p>
<ul>
<li>ChatServer:服务器端的唯一的类，作用就是通过Selector监听Read和Accept事件，并针对这些事件的类型，进行不同的处理，如连接、转发。</li>
<li>ChatClient:客户端，通过Selector监听Read和Connect事件。Read事件就是获取服务器转发的消息然后显示出来；Connect事件就是和服务器建立连接，建立成功后就可以发送消息。</li>
<li>UserInputHandler:专门等待用户输入的线程，和BIO没区别。</li>
</ul>
<h3 id="ChatServer"><a href="#ChatServer" class="headerlink" title="ChatServer"></a><strong>ChatServer</strong></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatServer</span> </span>&#123;</span><br><span class="line">    <span class="comment">//设置缓冲区的大小，这里设置为1024个字节</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> BUFFER = <span class="number">1024</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//Channel都要配合缓冲区进行读写，所以这里创建一个读缓冲区和一个写缓冲区</span></span><br><span class="line">    <span class="comment">//allocate()静态方法就是设置缓存区大小的方法</span></span><br><span class="line">    <span class="keyword">private</span> ByteBuffer read_buffer = ByteBuffer.allocate(BUFFER);</span><br><span class="line">    <span class="keyword">private</span> ByteBuffer write_buffer = ByteBuffer.allocate(BUFFER);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//为了监听端口更灵活，再不写死了，用一个构造函数设置需要监听的端口号</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> port;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ChatServer</span><span class="params">(<span class="keyword">int</span> port)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.port = port;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//创建ServerSocketChannel和Selector并打开</span></span><br><span class="line">        <span class="keyword">try</span> (ServerSocketChannel server = ServerSocketChannel.open(); Selector selector = Selector.open()) &#123;</span><br><span class="line">            <span class="comment">//【重点,实现NIO编程模型的关键】configureBlocking设置ServerSocketChannel为非阻塞式调用,Channel默认的是阻塞的调用方式</span></span><br><span class="line">            server.configureBlocking(<span class="keyword">false</span>);</span><br><span class="line">            <span class="comment">//绑定监听端口,这里不是给ServerSocketChannel绑定，而是给ServerSocket绑定，socket()就是获取通道原生的ServerSocket或Socket</span></span><br><span class="line">            server.socket().bind(<span class="keyword">new</span> InetSocketAddress(port));</span><br><span class="line"></span><br><span class="line">            <span class="comment">//把server注册到Selector并监听Accept事件</span></span><br><span class="line">            server.register(selector, SelectionKey.OP_ACCEPT);</span><br><span class="line">            System.out.println(<span class="string">&quot;启动服务器，监听端口:&quot;</span> + port);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                <span class="comment">//select()会返回此时触发了多少个Selector监听的事件</span></span><br><span class="line">                <span class="keyword">if</span>(selector.select()&gt;<span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="comment">//获取这些已经触发的事件,selectedKeys()返回的是触发事件的所有信息</span></span><br><span class="line">                    Set&lt;SelectionKey&gt; selectionKeys = selector.selectedKeys();</span><br><span class="line">                    <span class="comment">//循环处理这些事件</span></span><br><span class="line">                    <span class="keyword">for</span> (SelectionKey key : selectionKeys) &#123;</span><br><span class="line">                        handles(key, selector);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="comment">//处理完后清空selectedKeys，避免重复处理</span></span><br><span class="line">                    selectionKeys.clear();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//处理事件的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">handles</span><span class="params">(SelectionKey key, Selector selector)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">//当触发了Accept事件，也就是有客户端请求进来</span></span><br><span class="line">        <span class="keyword">if</span> (key.isAcceptable()) &#123;</span><br><span class="line">            <span class="comment">//获取ServerSocketChannel</span></span><br><span class="line">            ServerSocketChannel server = (ServerSocketChannel) key.channel();</span><br><span class="line">            <span class="comment">//然后通过accept()方法接收客户端的请求，这个方法会返回客户端的SocketChannel，这一步和原生的ServerSocket类似</span></span><br><span class="line">            SocketChannel client = server.accept();</span><br><span class="line">            client.configureBlocking(<span class="keyword">false</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//把客户端的SocketChannel注册到Selector，并监听Read事件</span></span><br><span class="line">            client.register(selector, SelectionKey.OP_READ);</span><br><span class="line">            System.out.println(<span class="string">&quot;客户端[&quot;</span> + client.socket().getPort() + <span class="string">&quot;]上线啦！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//当触发了Read事件，也就是客户端发来了消息</span></span><br><span class="line">        <span class="keyword">if</span> (key.isReadable()) &#123;</span><br><span class="line">            SocketChannel client = (SocketChannel) key.channel();</span><br><span class="line">            <span class="comment">//获取消息</span></span><br><span class="line">            String msg = receive(client);</span><br><span class="line">            System.out.println(<span class="string">&quot;客户端[&quot;</span> + client.socket().getPort() + <span class="string">&quot;]:&quot;</span> + msg);</span><br><span class="line">            <span class="comment">//把消息转发给其他客户端</span></span><br><span class="line">            sendMessage(client, msg, selector);</span><br><span class="line">            <span class="comment">//判断用户是否退出</span></span><br><span class="line">            <span class="keyword">if</span> (msg.equals(<span class="string">&quot;quit&quot;</span>)) &#123;</span><br><span class="line">                <span class="comment">//解除该事件的监听</span></span><br><span class="line">                key.cancel();</span><br><span class="line">                <span class="comment">//更新Selector</span></span><br><span class="line">                selector.wakeup();</span><br><span class="line">                System.out.println(<span class="string">&quot;客户端[&quot;</span> + client.socket().getPort() + <span class="string">&quot;]下线了！&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//编码方式设置为utf-8，下面字符和字符串互转时用得到</span></span><br><span class="line">    <span class="keyword">private</span> Charset charset = Charset.forName(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//接收消息的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> String <span class="title">receive</span><span class="params">(SocketChannel client)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">//用缓冲区之前先清空一下,避免之前的信息残留</span></span><br><span class="line">        read_buffer.clear();</span><br><span class="line">        <span class="comment">//把通道里的信息读取到缓冲区，用while循环一直读取，直到读完所有消息。因为没有明确的类似\n这样的结尾，所以要一直读到没有字节为止</span></span><br><span class="line">        <span class="keyword">while</span> (client.read(read_buffer) &gt; <span class="number">0</span>) ;</span><br><span class="line">        <span class="comment">//把消息读取到缓冲区后，需要转换buffer的读写状态，不明白的看看前面的Buffer的讲解</span></span><br><span class="line">        read_buffer.flip();</span><br><span class="line">        <span class="keyword">return</span> String.valueOf(charset.decode(read_buffer));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//转发消息的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">sendMessage</span><span class="params">(SocketChannel client, String msg, Selector selector)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        msg = <span class="string">&quot;客户端[&quot;</span> + client.socket().getPort() + <span class="string">&quot;]:&quot;</span> + msg;</span><br><span class="line">        <span class="comment">//获取所有客户端,keys()与前面的selectedKeys不同，这个是获取所有已经注册的信息，而selectedKeys获取的是触发了的事件的信息</span></span><br><span class="line">        <span class="keyword">for</span> (SelectionKey key : selector.keys()) &#123;</span><br><span class="line">            <span class="comment">//排除服务器和本客户端并且保证key是有效的，isValid()会判断Selector监听是否正常、对应的通道是保持连接的状态等</span></span><br><span class="line">            <span class="keyword">if</span> (!(key.channel() <span class="keyword">instanceof</span> ServerSocketChannel) &amp;&amp; !client.equals(key.channel()) &amp;&amp; key.isValid()) &#123;</span><br><span class="line">                SocketChannel otherClient = (SocketChannel) key.channel();</span><br><span class="line">                write_buffer.clear();</span><br><span class="line">                write_buffer.put(charset.encode(msg));</span><br><span class="line">                write_buffer.flip();</span><br><span class="line">                <span class="comment">//把消息写入到缓冲区后，再把缓冲区的内容写到客户端对应的通道中</span></span><br><span class="line">                <span class="keyword">while</span> (write_buffer.hasRemaining()) &#123;</span><br><span class="line">                    otherClient.write(write_buffer);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> ChatServer(<span class="number">8888</span>).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="ChatClient"><a href="#ChatClient" class="headerlink" title="ChatClient"></a><strong>ChatClient</strong></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatClient</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> BUFFER = <span class="number">1024</span>;</span><br><span class="line">    <span class="keyword">private</span> ByteBuffer read_buffer = ByteBuffer.allocate(BUFFER);</span><br><span class="line">    <span class="keyword">private</span> ByteBuffer write_buffer = ByteBuffer.allocate(BUFFER);</span><br><span class="line">    <span class="comment">//声明成全局变量是为了方便下面一些工具方法的调用，就不用try with resource了</span></span><br><span class="line">    <span class="keyword">private</span> SocketChannel client;</span><br><span class="line">    <span class="keyword">private</span> Selector selector;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Charset charset = Charset.forName(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span>  &#123;</span><br><span class="line">            client=SocketChannel.open();</span><br><span class="line">            selector=Selector.open();</span><br><span class="line">            client.configureBlocking(<span class="keyword">false</span>);</span><br><span class="line">            <span class="comment">//注册channel，并监听SocketChannel的Connect事件</span></span><br><span class="line">            client.register(selector, SelectionKey.OP_CONNECT);</span><br><span class="line">            <span class="comment">//请求服务器建立连接</span></span><br><span class="line">            client.connect(<span class="keyword">new</span> InetSocketAddress(<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>));</span><br><span class="line">            <span class="comment">//和服务器一样，不停的获取触发事件，并做相应的处理</span></span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                selector.select();</span><br><span class="line">                Set&lt;SelectionKey&gt; selectionKeys = selector.selectedKeys();</span><br><span class="line">                <span class="keyword">for</span> (SelectionKey key : selectionKeys) &#123;</span><br><span class="line">                    handle(key);</span><br><span class="line">                &#125;</span><br><span class="line">                selectionKeys.clear();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">catch</span> (ClosedSelectorException e)&#123;</span><br><span class="line">            <span class="comment">//当用户输入quit时，在send()方法中，selector会被关闭，而在上面的无限while循环中，可能会使用到已经关闭了的selector。</span></span><br><span class="line">            <span class="comment">//所以这里捕捉一下异常，做正常退出处理就行了。不会对服务器造成影响</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">handle</span><span class="params">(SelectionKey key)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">//当触发connect事件，也就是服务器和客户端建立连接</span></span><br><span class="line">        <span class="keyword">if</span> (key.isConnectable()) &#123;</span><br><span class="line">            SocketChannel client = (SocketChannel) key.channel();</span><br><span class="line">            <span class="comment">//finishConnect()返回true，说明和服务器已经建立连接。如果是false，说明还在连接中，还没完全连接完成</span></span><br><span class="line">            <span class="keyword">if</span>(client.finishConnect())&#123;</span><br><span class="line">                <span class="comment">//新建一个新线程去等待用户输入</span></span><br><span class="line">                <span class="keyword">new</span> Thread(<span class="keyword">new</span> UserInputHandler(<span class="keyword">this</span>)).start();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//连接建立完成后，注册read事件，开始监听服务器转发的消息</span></span><br><span class="line">            client.register(selector,SelectionKey.OP_READ);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//当触发read事件，也就是获取到服务器的转发消息</span></span><br><span class="line">        <span class="keyword">if</span>(key.isReadable())&#123;</span><br><span class="line">            SocketChannel client = (SocketChannel) key.channel();</span><br><span class="line">            <span class="comment">//获取消息</span></span><br><span class="line">            String msg = receive(client);</span><br><span class="line">            System.out.println(msg);</span><br><span class="line">            <span class="comment">//判断用户是否退出</span></span><br><span class="line">            <span class="keyword">if</span> (msg.equals(<span class="string">&quot;quit&quot;</span>)) &#123;</span><br><span class="line">                <span class="comment">//解除该事件的监听</span></span><br><span class="line">                key.cancel();</span><br><span class="line">                <span class="comment">//更新Selector</span></span><br><span class="line">                selector.wakeup();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//获取消息</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> String <span class="title">receive</span><span class="params">(SocketChannel client)</span> <span class="keyword">throws</span> IOException</span>&#123;</span><br><span class="line">        read_buffer.clear();</span><br><span class="line">        <span class="keyword">while</span> (client.read(read_buffer)&gt;<span class="number">0</span>);</span><br><span class="line">        read_buffer.flip();</span><br><span class="line">        <span class="keyword">return</span> String.valueOf(charset.decode(read_buffer));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//发送消息</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send</span><span class="params">(String msg)</span> <span class="keyword">throws</span> IOException</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(!msg.isEmpty())&#123;</span><br><span class="line">            write_buffer.clear();</span><br><span class="line">            write_buffer.put(charset.encode(msg));</span><br><span class="line">            write_buffer.flip();</span><br><span class="line">            <span class="keyword">while</span> (write_buffer.hasRemaining())&#123;</span><br><span class="line">                client.write(write_buffer);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(msg.equals(<span class="string">&quot;quit&quot;</span>))&#123;</span><br><span class="line">                selector.close();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> ChatClient().start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="UserInputHandler"><a href="#UserInputHandler" class="headerlink" title="UserInputHandler"></a><strong>UserInputHandler</strong></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserInputHandler</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line">    ChatClient client;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">UserInputHandler</span><span class="params">(ChatClient chatClient)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.client=chatClient;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        BufferedReader read=<span class="keyword">new</span> BufferedReader(</span><br><span class="line">                <span class="keyword">new</span> InputStreamReader(System.in)</span><br><span class="line">        );</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                String input=read.readLine();</span><br><span class="line">                client.send(input);</span><br><span class="line">                <span class="keyword">if</span>(input.equals(<span class="string">&quot;quit&quot;</span>))</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试运行：之前用的是win10的终端运行的，以后直接用IDEA运行，方便些。不过一个类同时运行多个，以实现多个客户端的场景，需要先做以下设置</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414152051636-927905062.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414152051636-927905062.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414152100412-1057042302.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414152100412-1057042302.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414152130678-206345317.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414152130678-206345317.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414152204091-2069873206.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414152204091-2069873206.png"></p>
<p>设置完后，就可以同时运行两个ChatClient了，上图中得Unnamed就是第二个ChatClient，选中后点击右边运行按钮就行了。效果如下：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414152412715-1986061599.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414152412715-1986061599.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414152438453-1030565064.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414152438453-1030565064.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200414152450583-1807958534.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200414152450583-1807958534.png"></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B64%EF%BC%9AAIO%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%EF%BC%8C%E8%81%8A%E5%A4%A9%E5%AE%A4%E7%BB%88%E6%9E%81%E6%94%B9/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/05/28/%E6%89%8B%E5%8A%A8%E6%90%AD%E5%BB%BAI%20O%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E6%A1%86%E6%9E%B64%EF%BC%9AAIO%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%EF%BC%8C%E8%81%8A%E5%A4%A9%E5%AE%A4%E7%BB%88%E6%9E%81%E6%94%B9/" class="post-title-link" itemprop="url">手动搭建I/O网络通信框架4：AIO编程模型，聊天室终极改造</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-05-28 19:39:32 / 修改时间：09:45:21" itemprop="dateCreated datePublished" datetime="2021-05-28T19:39:32+08:00">2021-05-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/Netty/" itemprop="url" rel="index"><span itemprop="name">Netty</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="手动搭建I-O网络通信框架4：AIO编程模型，聊天室终极改造"><a href="#手动搭建I-O网络通信框架4：AIO编程模型，聊天室终极改造" class="headerlink" title="手动搭建I/O网络通信框架4：AIO编程模型，聊天室终极改造"></a>手动搭建I/O网络通信框架4：AIO编程模型，聊天室终极改造</h1><blockquote>
<p>转载自 <a target="_blank" rel="noopener" href="https://www.cnblogs.com/lbhym/p/12720944.html">https://www.cnblogs.com/lbhym/p/12720944.html</a></p>
</blockquote>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>上一章讲到的NIO编程模型比较主流，非常著名的Netty就是基于NIO编程模型的。这一章说的是AIO编程模型，是<strong>异步非阻塞</strong>的。虽然同样实现的是聊天室功能，但是实现逻辑上稍微要比NIO和BIO复杂一点。不过理好整体脉络，会好理解一些。首先还是讲讲概念：</p>
<p>BIO和NIO的区别是阻塞和非阻塞，而AIO代表的是异步IO。在此之前只提到了阻塞和非阻塞，没有提到异步还是同步。可以用我在知乎上看到的一句话表示：【在处理 IO 的时候，阻塞和非阻塞都是同步 IO，只有使用了特殊的 API 才是异步 IO】。这些“特殊的API”下面会讲到。在说AIO之前，先总结一下阻塞非阻塞、异步同步的概念。</p>
<p><strong>阻塞和非阻塞，描述的是结果的请求</strong>。<strong>阻塞</strong>：在得到结果之前就一直呆在那，啥也不干，此时线程挂起，就如其名，线程被阻塞了。<strong>非阻塞</strong>：如果没得到结果就返回，等一会再去请求，直到得到结果为止。<strong>异步和同步，描述的是结果的发出</strong>，当调用方的请求进来。<strong>同步</strong>：在没获取到结果前就不返回给调用方，如果调用方是阻塞的，那么调用方就会一直等着。如果调用方是非阻塞的，调用方就会先回去，等一会再来问问得到结果没。<strong>异步</strong>：调用方一来，会直接返回，等执行完实际的逻辑后在通过回调函数把结果返回给调用方。</p>
<h1 id="AIO中的异步操作"><a href="#AIO中的异步操作" class="headerlink" title="AIO中的异步操作"></a><strong>AIO中的异步操作</strong></h1><h2 id="CompletionHandler"><a href="#CompletionHandler" class="headerlink" title="CompletionHandler"></a><strong>CompletionHandler</strong></h2><p>在AIO编程模型中，常用的API，如connect、accept、read、write都是支持异步操作的。当调用这些方法时，可以携带一个<strong>CompletionHandler</strong>参数，它会提供一些回调函数。这些回调函数包括:</p>
<ol>
<li>当这些操作成功时你需要怎么做；</li>
<li>如果这些操作失败了你要这么做。</li>
</ol>
<p>关于这个CompletionHandler参数，你只需要写一个类实现CompletionHandler口，并实现里面两个方法就行了。</p>
<p>那如何在调用connect、accept、read、write这四个方法时，传入CompletionHandler参数从而实现异步呢？下面分别举例这四个方法的使用。</p>
<p>先说说<code>Socket</code>和<code>ServerSocket</code>，在NIO中，它们变成了通道，配合缓冲区，从而实现了非阻塞。而在AIO中它们变成了异步通道。也就是<code>AsynchronousServerSocketChannel</code>和<code>AsynchronousSocketChannel</code>,下面例子中对象名分别是serverSocket和socket.</p>
<ul>
<li><p>accept：serverSocket.accept(attachment,handler)。</p>
<p>  handler就是实现了CompletionHandler接口并实现两个回调函数的类，它具体怎么写可以看下面的实战代码。attachment为handler里面可能需要用到的辅助数据，如果没有就填null。</p>
</li>
<li><p>read：socket.read(buffer,attachment,handler)。</p>
<p>  buffer是缓冲区，用以存放读取到的信息。后面两个参数和accept一样。</p>
</li>
<li><p>write：socket.write(buffer,attachment,handler)。</p>
<p>  和read参数一样。</p>
</li>
<li><p>connect：socket.connect(address,attachment,handler)。</p>
<p>  address为服务器的IP和端口，后面两个参数与前几个一样。</p>
</li>
</ul>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200417161130725-1186191947.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200417161130725-1186191947.png"></p>
<h2 id="Future"><a href="#Future" class="headerlink" title="Future"></a><strong>Future</strong></h2><p>既然说到了异步操作，除了使用实现CompletionHandler接口的方式，不得不想到<strong>Future</strong>。客户端逻辑较为简单，如果使用CompletionHandler的话代码反而更复杂，所以下面的实战客户端代码就会使用Future的方式。简单来说，Future表示的是异步操作未来的结果，怎么理解未来。比如，客户端调用read方法获取服务器发来得消息：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Future&lt;Integer&gt; readResult=clientChannel.read(buffer)</span><br></pre></td></tr></table></figure>

<p>Integer是read()的返回类型，此时变量readResult实际上并不一定有数据，而是表示read()方法未来的结果，这时候readResult有两个方法，<strong>isDone</strong>()：返回boolean，查看程序是否完成处理，如果返回true，有结果了，这时候可以通过get()获取结果。如果你不事先判断isDone()直接调用<strong>get</strong>()也行，只不过它是阻塞的。如果你不想阻塞，想在这期间做点什么，就用isDone()。</p>
<p>还有一个问题：<strong>这些handler的方法是在哪个线程执行的？</strong>serverSocket.accept这个方法肯定是在主线程里面调用的，而传入的这些回调方法其实是在其他线程执行的。在AIO中，会有一个<strong>AsynchronousChannelGroup</strong>，它和AsynchronousServerSocketChannel是绑定在一起的，<strong>它会为这些异步通道提供系统资源，线程就算其中一种系统资源</strong>，所以为了方便理解，我们暂时可以把他看作一个线程池，它会为这些handler分配线程，而不是在主线程中去执行。</p>
<h1 id="AIO编程模型"><a href="#AIO编程模型" class="headerlink" title="AIO编程模型"></a><strong>AIO编程模型</strong></h1><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200417161442138-580814692.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200417161442138-580814692.png"></p>
<p>上面只说了些零碎的概念，为了更好的理解，下面讲一讲大概的工作流程(主要针对服务器，客户端逻辑较为简单，代码注释也比较少，可以看前面几章)：</p>
<ol>
<li>首先做准备工作。跟NIO一样，先要创建好通道，只不过AIO是异步通道。然后创建好AsyncChannelGroup，可以选择自定义线程池。最后把AsyncServerSocket和AsyncChannelGroup绑定在一起，这样处于同一个AsyncChannelGroup里的通道就可以共享系统资源。</li>
<li>最后一步准备工作，创建好handler类，并实现接口和里面两个回调方法。（如图：客户端1对应的handler,里面的回调方法会实现读取消息和转发消息的功能；serverSocket的handler里的回调方法会实现accept功能。）</li>
<li>准备工作完成，当客户端1连接请求进来，客户端会马上回去，ServerSocket的异步方法会在连接成功后把客户端的SocketChannel存进在线用户列表，并利用客户端1的handler开始异步监听客户端1发送的消息。</li>
<li>当客户端1发送消息时，如果上一步中的handler成功监听到，就会回调成功后的回调方法，这个方法里会把这个消息转发给其他客户端。转发完成后，接着利用handler监听客户端1发送的消息。</li>
</ol>
<p>代码一共有三个类：</p>
<ul>
<li>ChatServer：功能基本上和上面讲的工作流程差不多，还会有一些工具方法，都比较简单，就不多说了，如：转发消息，客户端下线后从在线列表移除客户端等。</li>
<li>ChatClient：基本和前两章的BIO、NIO没什么区别，一个线程监听用户输入信息并发送，主线程异步的读取服务器信息。</li>
<li>UserInputHandler：监听用户输入信息的线程。</li>
</ul>
<h2 id="ChatServer"><a href="#ChatServer" class="headerlink" title="ChatServer"></a>ChatServer</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatServer</span> </span>&#123;</span><br><span class="line">    <span class="comment">//设置缓冲区字节大小</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> BUFFER = <span class="number">1024</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//声明AsynchronousServerSocketChannel和AsynchronousChannelGroup</span></span><br><span class="line">    <span class="keyword">private</span> AsynchronousServerSocketChannel serverSocketChannel;</span><br><span class="line">    <span class="keyword">private</span> AsynchronousChannelGroup channelGroup;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//在线用户列表。为了并发下的线程安全，所以使用CopyOnWriteArrayList</span></span><br><span class="line">    <span class="comment">//CopyOnWriteArrayList在写时加锁，读时不加锁，而本项目正好在转发消息时需要频繁读取.</span></span><br><span class="line">    <span class="comment">//ClientHandler包含每个客户端的通道，类型选择为ClientHandler是为了在write的时候调用每个客户端的handler</span></span><br><span class="line">    <span class="keyword">private</span> CopyOnWriteArrayList&lt;ClientHandler&gt; clientHandlerList;</span><br><span class="line">    <span class="comment">//字符和字符串互转需要用到，规定编码方式，避免中文乱码</span></span><br><span class="line">    <span class="keyword">private</span> Charset charset = Charset.forName(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//通过构造函数设置监听端口</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> port;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ChatServer</span><span class="params">(<span class="keyword">int</span> port)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.port = port;</span><br><span class="line">        clientHandlerList=<span class="keyword">new</span> CopyOnWriteArrayList&lt;&gt;();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             *创建一个线程池并把线程池和AsynchronousChannelGroup绑定，前面提到了AsynchronousChannelGroup包括一些系统资源，而线程就是其中一种。</span></span><br><span class="line"><span class="comment">             *为了方便理解我们就暂且把它当作线程池，实际上并不止包含线程池。如果你需要自己选定线程池类型和数量，就可以如下操作</span></span><br><span class="line"><span class="comment">             *如果不需要自定义线程池类型和数量，可以不用写下面两行代码。</span></span><br><span class="line"><span class="comment">             * */</span></span><br><span class="line">            ExecutorService executorService = Executors.newFixedThreadPool(<span class="number">10</span>);</span><br><span class="line">            channelGroup = AsynchronousChannelGroup.withThreadPool(executorService);</span><br><span class="line">            serverSocketChannel=AsynchronousServerSocketChannel.open(channelGroup);</span><br><span class="line">            serverSocketChannel.bind(<span class="keyword">new</span> InetSocketAddress(<span class="string">&quot;127.0.0.1&quot;</span>,port));</span><br><span class="line">            System.out.println(<span class="string">&quot;服务器启动：端口【&quot;</span>+port+<span class="string">&quot;】&quot;</span>);</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * AIO中accept可以异步调用，就用上面说到的CompletionHandler方式</span></span><br><span class="line"><span class="comment">             * 第一个参数是辅助参数，回调函数中可能会用上的，如果没有就填null;第二个参数为CompletionHandler接口的实现</span></span><br><span class="line"><span class="comment">             * 这里使用while和System.in.read()的原因：</span></span><br><span class="line"><span class="comment">             * while是为了让服务器保持运行状态，前面的NIO，BIO都有用到while无限循环来保持服务器运行，但是它们用的地方可能更好理解</span></span><br><span class="line"><span class="comment">             * System.in.read()是阻塞式的调用，只是单纯的避免无限循环而让accept频繁被调用，无实际业务功能。</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">                serverSocketChannel.accept(<span class="keyword">null</span>, <span class="keyword">new</span> AcceptHandler());</span><br><span class="line">                System.in.read();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(serverSocketChannel!=<span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    serverSocketChannel.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//AsynchronousSocketChannel为accept返回的类型，Object为辅助参数类型，没有就填Object</span></span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">AcceptHandler</span> <span class="keyword">implements</span> <span class="title">CompletionHandler</span>&lt;<span class="title">AsynchronousSocketChannel</span>,<span class="title">Object</span>&gt;</span>&#123;</span><br><span class="line">        <span class="comment">//如果成功，执行的回调方法</span></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">completed</span><span class="params">(AsynchronousSocketChannel clientChannel, Object attachment)</span> </span>&#123;</span><br><span class="line">            <span class="comment">//如果服务器没关闭，在接收完当前客户端的请求后，再次调用,以接着接收其他客户端的请求</span></span><br><span class="line">            <span class="keyword">if</span>(serverSocketChannel.isOpen())&#123;</span><br><span class="line">                serverSocketChannel.accept(<span class="keyword">null</span>,<span class="keyword">this</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//如果客户端的channel没有关闭</span></span><br><span class="line">            <span class="keyword">if</span>(clientChannel!=<span class="keyword">null</span>&amp;&amp;clientChannel.isOpen())&#123;</span><br><span class="line">                <span class="comment">//这个就是异步read和write要用到的handler,并传入当前客户端的channel</span></span><br><span class="line">                ClientHandler handler=<span class="keyword">new</span> ClientHandler(clientChannel);</span><br><span class="line">                <span class="comment">//把新用户添加到在线用户列表里</span></span><br><span class="line">                clientHandlerList.add(handler);</span><br><span class="line">                System.out.println(getPort(clientChannel)+<span class="string">&quot;上线啦！&quot;</span>);</span><br><span class="line">                ByteBuffer buffer=ByteBuffer.allocate(BUFFER);</span><br><span class="line">                <span class="comment">//异步调用read,第一个buffer是存放读到数据的容器，第二个是辅助参数。</span></span><br><span class="line">                <span class="comment">//因为真正的处理是在handler里的回调函数进行的，辅助参数会直接传进回调函数，所以为了方便使用，buffer就当作辅助参数</span></span><br><span class="line">                clientChannel.read(buffer,buffer,handler);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//如果失败，执行的回调方法</span></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">failed</span><span class="params">(Throwable exc, Object attachment)</span> </span>&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;连接失败&quot;</span>+exc);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">ClientHandler</span> <span class="keyword">implements</span> <span class="title">CompletionHandler</span>&lt;<span class="title">Integer</span>, <span class="title">ByteBuffer</span>&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">private</span> AsynchronousSocketChannel clientChannel;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">ClientHandler</span><span class="params">(AsynchronousSocketChannel clientChannel)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.clientChannel = clientChannel;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">completed</span><span class="params">(Integer result, ByteBuffer buffer)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(buffer!=<span class="keyword">null</span>)&#123;</span><br><span class="line">                <span class="comment">//如果read返回的结果小于等于0，而buffer不为空，说明客户端通道出现异常，做下线操作</span></span><br><span class="line">                <span class="keyword">if</span>(result&lt;=<span class="number">0</span>)&#123;</span><br><span class="line">                    removeClient(<span class="keyword">this</span>);</span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">//转换buffer读写模式并获取消息</span></span><br><span class="line">                    buffer.flip();</span><br><span class="line">                    String msg=String.valueOf(charset.decode(buffer));</span><br><span class="line">                    <span class="comment">//在服务器上打印客户端发来的消息</span></span><br><span class="line">                    System.out.println(getPort(clientChannel)+msg);</span><br><span class="line">                    <span class="comment">//把消息转发给其他客户端</span></span><br><span class="line">                    sendMessage(clientChannel,getPort(clientChannel)+msg);</span><br><span class="line">                    buffer=ByteBuffer.allocate(BUFFER);</span><br><span class="line"></span><br><span class="line">                    <span class="comment">//如果用户输入的是退出，就从在线列表里移除。否则接着监听这个用户发送消息</span></span><br><span class="line">                    <span class="keyword">if</span>(msg.equals(<span class="string">&quot;quit&quot;</span>))</span><br><span class="line">                        removeClient(<span class="keyword">this</span>);</span><br><span class="line">                    <span class="keyword">else</span></span><br><span class="line">                        clientChannel.read(buffer, buffer, <span class="keyword">this</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">failed</span><span class="params">(Throwable exc, ByteBuffer attachment)</span> </span>&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;客户端读写异常：&quot;</span>+exc);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//转发消息的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">sendMessage</span><span class="params">(AsynchronousSocketChannel clientChannel,String msg)</span></span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(ClientHandler handler:clientHandlerList)&#123;</span><br><span class="line">            <span class="keyword">if</span>(!handler.clientChannel.equals(clientChannel))&#123;</span><br><span class="line">                ByteBuffer buffer=charset.encode(msg);</span><br><span class="line">                <span class="comment">//write不需要buffer当辅助参数，因为写到客户端的通道就完事了，而读还需要回调函数转发给其他客户端。</span></span><br><span class="line">                handler.clientChannel.write(buffer,<span class="keyword">null</span>,handler);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//根据客户端channel获取对应端口号的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> String <span class="title">getPort</span><span class="params">(AsynchronousSocketChannel clientChannel)</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            InetSocketAddress address=(InetSocketAddress)clientChannel.getRemoteAddress();</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;客户端[&quot;</span>+address.getPort()+<span class="string">&quot;]:&quot;</span>;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;客户端[Undefined]:&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//移除客户端</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">removeClient</span><span class="params">(ClientHandler handler)</span></span>&#123;</span><br><span class="line">        clientHandlerList.remove(handler);</span><br><span class="line">        System.out.println(getPort(handler.clientChannel)+<span class="string">&quot;断开连接...&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span>(handler.clientChannel!=<span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                handler.clientChannel.close();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> ChatServer(<span class="number">8888</span>).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="ChatClient"><a href="#ChatClient" class="headerlink" title="ChatClient"></a>ChatClient</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChatClient</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> BUFFER = <span class="number">1024</span>;</span><br><span class="line">    <span class="keyword">private</span> AsynchronousSocketChannel clientChannel;</span><br><span class="line">    <span class="keyword">private</span> Charset charset = Charset.forName(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String host;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> port;</span><br><span class="line">    <span class="comment">//设置服务器IP和端口</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ChatClient</span><span class="params">(String host, <span class="keyword">int</span> port)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.host = host;</span><br><span class="line">        <span class="keyword">this</span>.port = port;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            clientChannel = AsynchronousSocketChannel.open();</span><br><span class="line">            <span class="comment">//连接服务器</span></span><br><span class="line">            Future&lt;Void&gt; future = clientChannel.connect(<span class="keyword">new</span> InetSocketAddress(host, port));</span><br><span class="line">            future.get();</span><br><span class="line">            <span class="comment">//新建一个线程去等待用户输入</span></span><br><span class="line">            <span class="keyword">new</span> Thread(<span class="keyword">new</span> UserInputHandler(<span class="keyword">this</span>)).start();</span><br><span class="line">            ByteBuffer buffer=ByteBuffer.allocate(BUFFER);</span><br><span class="line">            <span class="comment">//无限循环让客户端保持运行状态</span></span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">                <span class="comment">//获取服务器发来的消息并存入到buffer</span></span><br><span class="line">                Future&lt;Integer&gt; read=clientChannel.read(buffer);</span><br><span class="line">                <span class="keyword">if</span>(read.get()&gt;<span class="number">0</span>)&#123;</span><br><span class="line">                    buffer.flip();</span><br><span class="line">                    String msg=String.valueOf(charset.decode(buffer));</span><br><span class="line">                    System.out.println(msg);</span><br><span class="line">                    buffer.clear();</span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">//如果read的结果小于等于0说明和服务器连接出现异常</span></span><br><span class="line">                    System.out.println(<span class="string">&quot;服务器断开连接&quot;</span>);</span><br><span class="line">                    <span class="keyword">if</span>(clientChannel!=<span class="keyword">null</span>)&#123;</span><br><span class="line">                        clientChannel.close();</span><br><span class="line">                    &#125;</span><br><span class="line">                    System.exit(-<span class="number">1</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException | InterruptedException | ExecutionException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send</span><span class="params">(String msg)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (msg.isEmpty())</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        ByteBuffer buffer = charset.encode(msg);</span><br><span class="line">        Future&lt;Integer&gt; write=clientChannel.write(buffer);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//获取发送结果，如果get方法发生异常说明发送失败</span></span><br><span class="line">            write.get();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ExecutionException|InterruptedException e) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;消息发送失败&quot;</span>);</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> ChatClient(<span class="string">&quot;127.0.0.1&quot;</span>,<span class="number">8888</span>).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="UserInputHandler"><a href="#UserInputHandler" class="headerlink" title="UserInputHandler"></a>UserInputHandler</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserInputHandler</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line">    ChatClient client;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">UserInputHandler</span><span class="params">(ChatClient chatClient)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.client=chatClient;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        BufferedReader read=<span class="keyword">new</span> BufferedReader(</span><br><span class="line">                <span class="keyword">new</span> InputStreamReader(System.in)</span><br><span class="line">        );</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                String input=read.readLine();</span><br><span class="line">                client.send(input);</span><br><span class="line">                <span class="keyword">if</span>(input.equals(<span class="string">&quot;quit&quot;</span>))</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行测试:</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200417164605075-508533365.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200417164605075-508533365.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200417164620441-2122640649.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200417164620441-2122640649.png"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/1383122-20200417164642458-1627643597.png" alt="https://img2020.cnblogs.com/blog/1383122/202004/1383122-20200417164642458-1627643597.png"></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/05/26/Netty%E4%B9%8BChannelOption/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/05/26/Netty%E4%B9%8BChannelOption/" class="post-title-link" itemprop="url">Netty之ChannelOption</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-05-26 19:16:14 / 修改时间：19:22:05" itemprop="dateCreated datePublished" datetime="2021-05-26T19:16:14+08:00">2021-05-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/netty/" itemprop="url" rel="index"><span itemprop="name">netty</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>在用netty作为底层网络通信的时候关于ChannelOption的参数让我一直模糊不清楚，于是去看一下linux网络编程，发现ChannelOption的各种属性在套接字选项中都有对应</p>
<p>下面简单的总结一下ChannelOption的含义已及使用的场景</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/blog/2021/05/26/Netty%E4%B9%8BChannelOption/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/05/20/%E6%B5%B7%E9%87%8F%E6%95%B0%E6%8D%AE%E7%9A%84%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AE%BF%E9%97%AE%E7%93%B6%E9%A2%88%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%20-%20%E6%95%B0%E6%8D%AE%E5%88%87%E5%88%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/blog/2021/05/20/%E6%B5%B7%E9%87%8F%E6%95%B0%E6%8D%AE%E7%9A%84%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AE%BF%E9%97%AE%E7%93%B6%E9%A2%88%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%20-%20%E6%95%B0%E6%8D%AE%E5%88%87%E5%88%86/" class="post-title-link" itemprop="url">海量数据的存储与访问瓶颈解决方案 - 数据切分</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-05-20 21:09:45" itemprop="dateCreated datePublished" datetime="2021-05-20T21:09:45+08:00">2021-05-20</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-06-01 09:19:34" itemprop="dateModified" datetime="2021-06-01T09:19:34+08:00">2021-06-01</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><p>在当今这个时代，人们对互联网的依赖程度非常高，也因此产生了大量的数据，企业视这些数据为瑰宝。而这些被视为瑰宝的数据为我们的系统带来了很大的烦恼。这些海量数据的存储与访问成为了系统设计与使用的瓶颈，而这些数据往往存储在数据库中，传统的数据库存在着先天的不足，即单机（单库）性能瓶颈，并且扩展起来非常的困难。在当今的这个大数据时代，我们急需解决这个问题。如果单机数据库易于扩展，数据可切分，就可以避免这些问题，但是当前的这些数据库厂商，包括开源的数据库MySQL在内，提供这些服务都是需要收费的，所以我们转向一些第三方的软件，使用这些软件做数据的切分，将原本在一台数据库上的数据，分散到多台数据库当中，降低每一个单体数据库的负载。那么我们如何做数据切分呢？</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/blog/2021/05/20/%E6%B5%B7%E9%87%8F%E6%95%B0%E6%8D%AE%E7%9A%84%E5%AD%98%E5%82%A8%E4%B8%8E%E8%AE%BF%E9%97%AE%E7%93%B6%E9%A2%88%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%20-%20%E6%95%B0%E6%8D%AE%E5%88%87%E5%88%86/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/blog/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/blog/">1</a><span class="page-number current">2</span><a class="page-number" href="/blog/page/3/">3</a><span class="space">&hellip;</span><a class="page-number" href="/blog/page/19/">19</a><a class="extend next" rel="next" href="/blog/page/3/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script>

  
<script src="/blog/js/local-search.js"></script>






  





</body>
</html>
